假设我们有一个包含以下内容的4字节文件
00 00 00 00
我想修改前两个字节来说
FF AA 00 00
如何使用vbscript完成此操作?使用vbscript的二进制IO参考也很不错。
答案 0 :(得分:1)
您可以查看此问题答案中的示例:Read and write binary file in VBscript
我不知道这在实践中有多好(mid函数可能会破坏结果),但它似乎适用于我使用以下代码:
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