我正在尝试读取二进制文件并编辑数据,然后将文件作为新文件写回。读取文件,写入文件不是问题......它工作正常。
但是,无论我尝试修改二进制数据的任何方式,都会出现错误:“VBScript 运行时错误:类型不匹配”
例如,我正在读取一个文件并将第三个字节修改为“@”(at-sign)。
Option Explicit
Const ADTYPEBINARY = 1
Const ADSAVECREATEOVERWRITE = 2
Dim gstrInputFileName
Dim gstrOutputFileName
Dim gobjBinaryInputStream
Dim gobjBinaryOutputStream
Dim gbytearrayReadBinaryFile
gstrInputFileName = "some-binary-file.bin"
gstrOutputFileName = "output-binary-filename.bin"
WScript.Echo "Reading the file data"
Set gobjBinaryInputStream = CreateObject("ADODB.Stream")
gobjBinaryInputStream.Type = ADTYPEBINARY
gobjBinaryInputStream.Open
gobjBinaryInputStream.LoadFromFile gstrInputFileName
gbytearrayReadBinaryFile = gobjBinaryInputStream.Read
gobjBinaryInputStream.Close
Set gobjBinaryInputStream = Nothing
WScript.Echo "Modifying the file data"
' These all fail... "Type mismatch"
'' pwwqbytearrayReadBinaryFile(3) = &H40
'' pwwqbytearrayReadBinaryFile(3) = ChrB(65)
'' pwwqbytearrayReadBinaryFile(3) = Chr(65)
'' pwwqbytearrayReadBinaryFile(3) = AscB("@")
'' pwwqbytearrayReadBinaryFile(3) = Asc("@")
'' pwwqbytearrayReadBinaryFile(3) = "@"
'' pwwqbytearrayReadBinaryFile(3) = 65
WScript.Echo "Writing the file data"
Set gobjBinaryOutputStream = CreateObject("ADODB.Stream")
gobjBinaryOutputStream.Type = ADTYPEBINARY
gobjBinaryOutputStream.Open
gobjBinaryOutputStream.Write gbytearrayReadBinaryFile
gobjBinaryOutputStream.SaveToFile gstrOutputFileName, ADSAVECREATEOVERWRITE
gobjBinaryOutputStream.Close
Set gobjBinaryOutputStream = Nothing
WScript.Echo
WScript.Quit