Тема: VBS: бинарный файл в base64
Всем привет. Решал вопрос, озвученный в заголовке. Вопрос: будет ли этот код работать во всех Windows начиная с XP.
Если у кого есть возможность, сообщите пожалуйста если ошибка вылетит. Вроде бы все должно, но мало ли. У меня на 8.1 х64 работает как надо.
' converter of binary into base64 and vise versa
errString = "Examples: " & vbCrLf & _
"- binary into base64: " & WScript.ScriptName & _
" -e myPicture.jpeg myDll.dll" & vbCrLf & _
"- base64 into binary: " & WScript.ScriptName & _
" -d myPicture.jpeg.b64 myDll.dll.b64"
If WScript.Arguments.Count < 2 Then
WScript.Echo "Error: Not enough arguments" & vbCrLf & errString
WScript.Quit
End If
wa = Right(LCase(WScript.Arguments(0)), 1)
If Not (wa = "e" Or wa = "d") Then
WScript.Echo "Error: Invalid named arguments" & vbCrLf & errString
WScript.Quit
End If
Set fso = CreateObject("Scripting.FileSystemObject")
For i=1 To WScript.Arguments.Count - 1
If Not fso.FileExists(WScript.Arguments(i)) Then
WScript.Echo "Error: File " & WScript.Arguments(i) & " not found" & vbCrLf & errString
WScript.Quit
End If
If wa = "e" Then WScript.Echo toBase64(WScript.Arguments(i))
If wa = "d" Then WScript.Echo toBinary(WScript.Arguments(i))
Next
Function toBase64(file)
With CreateObject("ADODB.Stream")
.Type = 1
.Open
.LoadFromFile(file)
bytes = .Read
End With
With CreateObject("Microsoft.XMLDOM").createElement("tmp")
.DataType = "bin.base64"
.NodeTypedValue = bytes
base64 = .Text
End With
newfile = fso.GetFileName(fso.GetFile(file)) & ".b64"
fso.CreateTextFile(newfile, True).Write(base64)
toBase64 = "toBase64: " & file & " >>> " & newfile & " : OK"
End Function
Function toBinary(file)
With fso.OpenTextFile(file, 1)
base64 = .ReadAll
.Close
End With
With CreateObject("Microsoft.XMLDOM").createElement("tmp")
.DataType = "bin.base64"
.Text = base64
bytes = .NodeTypedValue
End With
With CreateObject("ADODB.Stream")
.Type = 1
.Open
.Write bytes
newfile = "new-" & Replace(fso.GetFileName(fso.GetFile(file)), ".b64", vbNullString)
.SaveToFile newfile, 2
End With
toBinary = "toBinary: " & file & " >>> " & newfile & " : OK"
End Function

