在VBscript中读写二进制文件 [英] Read and write binary file in VBscript

查看:31
本文介绍了在VBscript中读写二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前使用过 ADODB.Stream 来读写二进制文件,这里是那个链接

I used earlier ADODB.Stream to read and to write binary file here is the link for that

如何使用 ADODB.stream 连接二进制文件在 VB 脚本中

它工作正常,唯一的问题是 Windows 2003 服务器上禁用了 ADODB.stream,

it works fine the only problem is ADODB.stream is disabled on windows 2003 server,

是否有另一种方法可以以二进制模式读取 3 个文件并将它们连接起来或将它们存储在 VBscript 中的一个文件中

Is there another way i can read 3 files in binary mode and concatenate them or store them in one file in VBscript

谢谢日

推荐答案

根据 Luc125 和 Alberto 的回答,这里有 2 个重新设计和简化的函数:

Based on Luc125 and Alberto answers here are the 2 reworked and simplified functions:

读取函数

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

这篇关于在VBscript中读写二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆