Wednesday, September 26, 2007

Import from bin64 to binary file with VBScript

'
' www.pullnews.com
' Save this code into file ImportFromBin64.vbs
'

If WScript.Arguments.Count= 0 Then
      MsgBox "Usage instruction:" & vbCrLf & _
      "Drag source file and drop on vbs file to process"
      WScript.Quit
End If

Set fso = CreateObject("Scripting.FileSystemObject")
sInFName = WScript.Arguments(0)
sPath = fso.GetParentFolderName (WScript.ScriptFullName)
ConvertBin64ToBinary sInFName

' Convert bin64 from xml file into binary file
Sub ConvertBin64ToBinary(sInFileName)
    Set xmlDoc = CreateObject("microsoft.xmldom")
    xmlDoc.async = False
    'Load xml doc
    If xmlDoc.Load(sInFName) = False Then
        MsgBox "Invalid XML request"
    Else
        Set oNode = xmlDoc.selectSingleNode("DATA")
        sOutFName = oNode.getAttribute("FileName")
        SaveBinaryData sPath & "\" & sOutFName, _
            oNode.nodeTypedValue
    End If
End Sub

' Save binary buffer to file
Function SaveBinaryData(FileName,ByteArray)
    Const adTypeBinary = 1
    Const adSaveCreateOverWrite = 2
    'Create Stream object
    Dim BinaryStream
    Set BinaryStream = CreateObject("ADODB.Stream")
    BinaryStream.Type = adTypeBinary
    BinaryStream.Open
    'Write binary data to stream object
    BinaryStream.Write ByteArray
    'Save to file
    BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function

No comments: