用VBscript读写二进制文件

用VBscript读写二进制文件,vbscript,Vbscript,我使用前面的ADODB.Stream来读取和写入二进制文件,这里是链接 它工作正常唯一的问题是在windows 2003 server上禁用了ADODB.stream 有没有其他方法可以在二进制模式下读取3个文件并将它们连接起来,或者将它们存储在VBscript中的一个文件中 多谢各位 ADODB stream对象是VBScript读取二进制流的唯一本地方法。如果禁用ADODB,则需要安装其他第三方组件以提供相同的功能。一年前我遇到过类似问题。我们知道TextStream对象用于ANSI或Un

我使用前面的ADODB.Stream来读取和写入二进制文件,这里是链接

它工作正常唯一的问题是在windows 2003 server上禁用了ADODB.stream

有没有其他方法可以在二进制模式下读取3个文件并将它们连接起来,或者将它们存储在VBscript中的一个文件中

多谢各位
ADODB stream对象是VBScript读取二进制流的唯一本地方法。如果禁用ADODB,则需要安装其他第三方组件以提供相同的功能。

一年前我遇到过类似问题。我们知道TextStream对象用于ANSI或Unicode文本数据,而不是二进制数据;如果流是二进制的,则其.readAll()方法会生成损坏的输出。但有解决办法。将字符逐个读取到数组中效果很好。这应该允许您将二进制数据读入VB字符串,并将其写回磁盘。当进一步操作这些二进制字符串时,不要忘记某些操作可能会导致断开的字符串,因为它们只用于文本。例如,我总是在使用二进制字符串之前将其转换为整数数组。
Function readBinary(path)
Function readBinary(path)
Dim a
Dim fso
Dim file
Dim i
Dim ts
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.getFile(path)
If isNull(file) Then
    MsgBox("File not found: " & path)
    Exit Function
End If
Set ts = file.OpenAsTextStream()
a = makeArray(file.size)
i = 0
' Do not replace the following block by readBinary = by ts.readAll(), it would result in broken output, because that method is not intended for binary data 
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 Dim ts Set fso = CreateObject("Scripting.FileSystemObject") On Error Resume Next Set ts = fso.createTextFile(path) If Err.number <> 0 Then MsgBox(Err.message) Exit Sub End If On Error GoTo 0 ts.Write(bstr) ts.Close End Sub

Function makeArray(n) ' Small utility function Dim s s = Space(n) makeArray = Split(s," ") End Function
暗淡的 模糊fso 暗文件 昏暗的我 暗淡的 设置fso=CreateObject(“Scripting.FileSystemObject”) Set file=fso.getFile(路径) 如果isNull(文件),则 MsgBox(“未找到文件:”&path) 退出功能 如果结束 设置ts=file.OpenAsTextStream() a=makeArray(file.size) i=0 '不要用readBinary=by ts.readAll()替换以下块,这将导致输出中断,因为该方法不适用于二进制数据 而不是ts.atEndOfStream a(i)=ts.read(1) i=i+1 温德 关闭 readBinary=Join(a,“”) 端函数

子写二进制(bstr,路径) 模糊fso 暗淡的 设置fso=CreateObject(“Scripting.FileSystemObject”) 出错时继续下一步 设置ts=fso.createTextFile(路径) 如果错误号为0,则 MsgBox(错误消息) 出口接头 如果结束 错误转到0 ts.Write(bstr) 关闭 端接头

函数makeArray(n)'小实用程序函数 暗淡的 s=空间(n) makeArray=Split(s,“”) 端函数

可以同时读取所有字节:

Set FS = CreateObject("Scripting.FileSystemObject")
Set fil = FS.GetFile(filename)
fpga = fil.OpenAsTextStream().Read(file.Size)

根据Luc125和Alberto的回答,这里有两个经过修改和简化的功能:

读取功能

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
写函数

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
函数writeBinary(strBinary,strPath)
Dim of so:Set of so=CreateObject(“Scripting.FileSystemObject”)
'以下行:检查是否可以进行写访问!
暗色oTxtStream
出错时继续下一步
设置oTxtStream=oFSO.createTextFile(strPath)
如果错误号为0,则MsgBox(错误消息):退出函数
错误转到0
设置oTxtStream=Nothing
'结束写访问检查
使用of so.createTextFile(strPath)
.写入(标准二进制)
.结束
以
端函数

ADODB stream对象是VBScript读取二进制流的唯一本机方法

Const TypeBinary = 1
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Function readBytes(file)

     Dim inStream: Set inStream = WScript.CreateObject("ADODB.Stream") ' ADODB stream object used

     inStream.Open ' open with no arguments makes the stream an empty container 
     inStream.type= TypeBinary
     inStream.LoadFromFile(file)
     readBytes = inStream.Read()

End Function

Sub writeBytes(file, bytes)

    Dim binaryStream: Set binaryStream = CreateObject("ADODB.Stream")

    binaryStream.Type = TypeBinary
    binaryStream.Open 'Open the stream and write binary data
    binaryStream.Write bytes
    binaryStream.SaveToFile file, ForWriting 'Save binary data to disk

End Sub

读取3个文件并连接到一个文件(不带
ADODB
):

在Win 10上对音频、视频、图像、zip存档和pdf(二进制文件)进行测试,用于二进制文件复制、编辑、拆分、连接、修补和(字节级)加密、编码和压缩


有关二进制文件修补的详细信息,请参阅。

是否需要此行
Dim oTxtStream:Set oTxtStream=oFSO.createTextFile(strPath)
假设该行检查文件的写访问权限。如果删除它,在某些情况下脚本可能会崩溃。虽然这在某些情况下有效,但通常是一种不好的做法。FSO设计用于标准文本流。此解决方案不是防弹的,在某些环境中确实会失败。最好还是坚持使用ADODB,这是一种为处理二进制流而设计的本地方法。没有额外的开销,而且效率更高。双赢。你是说读取(文件大小)?FSO不能可靠地处理二进制数据。@Nilpo:你为什么只对我的答案投票?您做了哪些测试来证明您的观点?众所周知,.read和.write的行为取决于系统语言环境。它在使用英语语言的国家可靠地工作。在其他国家,它不起作用。不需要安装任何外部设备;没有ADODB仍然可以完成;我一直在读/写二进制文件,没有it@ZimbaFSO无法可靠地处理二进制数据。它是为文本流设计的。ADODB是唯一设计用于处理二进制数据流的本机方法。
File
&
FileSystemObject
都可以处理二进制数据。问题是在没有ADODB的情况下寻求“另一种方式”。我在Win 10上测试了音频、视频、图像、zip存档和pdf。您的方法只在ADODB上工作,没有外部下载。本机FSO对象的OpenAsTextStream方法不支持二进制流。它不能可靠地处理二进制文件。@Nilpo:当然它支持二进制文件;我在Win 10上对音频、视频、图像、zip存档和pdf进行了测试。读和写的行为取决于系统区域设置。它在使用英语语言的国家可靠地工作。在其他国家,它不起作用。@david:fso可以“”,但文本字符串函数不起作用,因为“null截断字符串”,而二进制数据包含字符串。这个问题涉及二进制文件(单字节ASCII/ANSI字符),它可以处理。错误源于多字节/unicode代码页中缺少代码点,这些代码页是为适应其他语言而进行的字符扩展。为这些语言环境的抽象和语言环境映射/检测开发了一个“高级、易于使用的界面”(也可以使用
GetLocale
WMI
(在VBS之后发布))。VBScript受到本地文件系统访问的限制。这种解决方法不是一种好的做法。它不能处理所有的代码页。你真的在处理一个边缘案件,我不会把它投入生产。
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists("out.bin") Then oFSO.DeleteFile("out.bin")
Dim outFile : Set outFile = oFSO.OpenTextFile("out.bin", 8, true)

' 3 input files to concatenate
Dim oFS1 : Set oFS1 = oFSO.GetFile("in1.bin")
Dim oFS2 : Set oFS2 = oFSO.GetFile("in2.bin")
Dim oFS3 : Set oFS3 = oFSO.GetFile("in3.bin")

Dim read1 : Set read1 = oFS1.OpenAsTextStream()
Dim read2 : Set read2 = oFS2.OpenAsTextStream()
Dim read3 : Set read3 = oFS3.OpenAsTextStream()

Dim write1 : write1 = read1.Read(oFS1.Size)
read1.Close
outFile.write(write1)

Dim write2 : write2 = read2.Read(oFS2.Size)
read2.Close
outFile.write(write2)

Dim write3 : write3 = read3.Read(oFS3.Size)
read3.Close
outFile.write(write3)

outFile.Close