Read and write binary file in VBscript

vbscript

I used earlier ADODB.Stream to read and to write binary file here is the link for that

How to concatenate binary file using ADODB.stream in VBscript

it works fine the only problem is ADODB.stream is disabled on windows 2003 server,

Is there another way i can read 3 files in binary mode and concatenate them or store them in one file in VBscript

thank you
Jp

Best Answer

Based on Luc125 and Alberto answers here are the 2 reworked and simplified functions:

The Read function

Function readBinary(strPath)

    Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    Dim oFile: Set oFile = oFSO.GetFile(strPath)

    If IsNull(oFile) Then MsgBox("File not found: " & strPath) : Exit Function

    With oFile.OpenAsTextStream()
        readBinary = .Read(oFile.Size)
        .Close
    End With

End Function

The Write function

Function writeBinary(strBinary, strPath)

    Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")

    ' below lines pupose: checks that write access is possible!
    Dim oTxtStream

    On Error Resume Next
    Set oTxtStream = oFSO.createTextFile(strPath)

    If Err.number <> 0 Then MsgBox(Err.message) : Exit Function
    On Error GoTo 0

    Set oTxtStream = Nothing
    ' end check of write access

    With oFSO.createTextFile(strPath)
        .Write(strBinary)
        .Close
    End With

End Function
Related Topic