在二进制流中搜索和替换

时间:2015-05-19 09:04:30

标签: vbscript binarystream

我正在尝试更改字节流数组中的值。我正在寻找Null值,我想将其更改为空格。当我尝试访问该数组时,收到错误消息“Type Mismatch”。

我的VBS代码:

Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Const adSaveCreateNotExist=1
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
Dim InputFile
InputFile="C:\Users\oferbe\Documents\Tfachut\prepr\Testinput.txt"

'Specify stream type - we want To get binary data.
BinaryStream.Type = adTypeBinary

'Open the stream
BinaryStream.Open
'Load the file data from disk To stream object
BinaryStream.LoadFromFile InputFile

'Open the stream And get binary data from the object
ReadBinaryFile = BinaryStream.Read
BinaryStream.Close

For i = 0 to UBound(ReadBinaryFile)
  If ReadBinaryFile(i)=00 Then ReadBinaryFile(i)=20
Next

BinaryStream.Open
'BinaryStream.Write ByteArray
BinaryStream.Write ReadBinaryFile
Dim OutPutFile
OutPutFile="C:\Users\oferbe\Documents\Tfachut\prepr\Ofer"
'Save binary data To disk
BinaryStream.SaveToFile OutPutFile, adSaveCreateOverWrite

1 个答案:

答案 0 :(得分:2)

Read操作返回一个字节数组,它基本上是一个二进制字符串,具有VBScript数组的某些属性,但不是全部。您最好将二进制流作为常规字符串读取:

inputFile  = "C:\path\to\your\input.bin"
outputFile = "C:\path\to\your\output.bin"

Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2
stream.Charset = "Windows-1252"
stream.LoadFromFile inputFile
data = stream.ReadText
stream.Close

data = Replace(data, Chr(0), Chr(32))

stream.Open
stream.Type = 2
stream.Charset = "Windows-1252"
stream.WriteText data
stream.SaveToFile outputFile, 2
stream.Close

Set stream = Nothing