Reputation: 19546
Suppose we have a 4-byte file with the following contents
00 00 00 00
I want to modify the first two bytes to say
FF AA 00 00
How can I accomplish this with vbscript? A reference for binary IO using vbscript would also be nice.
Upvotes: 1
Views: 1380
Reputation: 1478
You could take a look at the example in the answer to this question: Read and write binary file in VBscript
I don't know how well this will work in practice (the mid function may mangle the results), but it seems to work here for me using the following code:
Option Explicit
Dim data
data = readBinary("C:\test.file")
' CHR(255) = FF, CHR(170) = AA
data = Chr(255)&Chr(170) & Mid(data, 3, Len(data) - 2)
writeBinary data,"C:\newtest.file"
Function readBinary(path)
Dim a, fso, file, i, ts
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.getFile(path)
If isNull(file) Then
wscript.echo "File not found: " & path
Exit Function
End If
Set ts = file.OpenAsTextStream()
a = makeArray(file.size)
i = 0
While Not ts.atEndOfStream
a(i) = ts.read(1)
i = i + 1
Wend
ts.close
readBinary = Join(a,"")
End Function
Sub writeBinary(bstr, path)
Dim fso, ts
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set ts = fso.createTextFile(path)
If Err.number <> 0 Then
wscript.echo Err.message
Exit Sub
End If
On Error GoTo 0
ts.Write(bstr)
ts.Close
End Sub
Function makeArray(n)
Dim s
s = Space(n)
makeArray = Split(s," ")
End Function
Upvotes: 2