Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Скриптовые языки администрирования Windows (http://forum.oszone.net/forumdisplay.php?f=102)
-   -   VBS. Формат суммы (http://forum.oszone.net/showthread.php?t=307834)

alex1985khv 12-11-2015 02:46 2573918

VBS. Формат суммы
 
Добрый день всем. Есть список из сумм. Примеры:

000000059900
000000010200
000000155005
....
....

Всегда длиной 12. Нужно преобразовать их в такой вид, на примерах выше:
599,00
102,00
1550,05

Т.е. обязательно удаляются все нули слева. И ставится запятая (копейки двух последних знаков).

Iska 12-11-2015 04:03 2573921

Попробуйте так:
Скрытый текст
Код:

Option Explicit

Const ForWriting  = 2

Dim strSourceFile
Dim strContent


If WScript.Arguments.Count = 1 Then
        strSourceFile = WScript.Arguments.Item(0)
       
        With WScript.CreateObject("Scripting.FileSystemObject")
                If .FileExists(strSourceFile) Then
                        With .OpenTextFile(strSourceFile)
                                strContent = .ReadAll()
                                .Close
                        End With
                       
                        With WScript.CreateObject("VBScript.RegExp")
                                .Global = True
                                .Pattern = "0*(\d*)(\d)(\d{2})"
                               
                                If .Test(strContent) Then
                                        strContent = .Replace(strContent, "$1$2,$3")
                                Else
                                        WScript.Echo "Can't find pattern [" & .Pattern & "]."
                                        WScript.Quit 3
                                End If
                        End With
                       
                        .CopyFile strSourceFile, strSourceFile & ".bak"
                       
                        With .OpenTextFile(strSourceFile, ForWriting)
                                .Write strContent
                                .Close
                        End With
                Else
                        WScript.Echo "Can't find source file [" & strSourceFile & "]."
                        WScript.Quit 2
                End If
        End With
Else
        WScript.Echo "Usage: cscript.exe //nologo """ & WScript.ScriptName & """ <Source file>"
        WScript.Quit 1
End If

WScript.Quit 0


Исходный файл указывается аргументом скрипта. Также можно просто перетянуть исходный файл на скрипт в Проводнике.

greg zakharov 12-11-2015 10:06 2573958

Можно оформить и как бат:
Код:

0</* :
@cscript /nologo /e:jscript "%~f0" %1&exit /b */0;
(function(file) {
  var s, l, r = [];
 
  try {
    with (new ActiveXObject('Scripting.FileSystemObject')) {
      s = OpenTextFile(file, 1);
      while (!s.atEndOfStream) {
        l = (Number(s.ReadLine()) / 100).toString();
        r.push(l.match(/\./) === null ? l + '.00' : l);
      }
      s.Close();
     
      s = OpenTextFile(file, 2);
      for (var i = 0; i < r.length; i++) {
        s.WriteLine(r[i]);
      }
      s.Close();
    }
  }
  catch (e) { WScript.echo(e); }
}(
  WScript.Arguments.length !== 1
  ? (function() {
    WScript.echo('File has not been specified correctly.');
    WScript.Quit(1);
  }()) : WScript.Arguments.Unnamed(0)
));


gora 12-11-2015 15:48 2574050

Код:

oldN = 000000059900
oldN = 000000155005

newN = oldN \ 100 & "," & ("0" And (oldN Mod 100) < 10) & oldN Mod 100

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Popup oldN & vbCR & newN



Время: 04:41.

Время: 04:41.
© OSzone.net 2001-