Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read binary file in vbs

Status
Not open for further replies.

AxelS

Technical User
Jan 29, 2002
4
DE
Hi All,

I have a problem in vbs to read a binary file. Have everybody a solution for that ?

For Text files I have the following solution, but how does this work with binary files (images...) ??

if len (filename) > 0 then
answer= MsgBox ("Do you want to send this file ? "& vbCrlf & filename, vbYesNo + vbQuestion)
if answer= vbYes then
set fs= CreateObject("Scripting.FileSystemObject")
if fs.FileExists(filename) then
set data= fs.OpenTextFile(filename, 1, false, -2)
inhalt= data.ReadAll
data.close
else
MSgBox "The File" & filename & "don`t exists !",vbExclamation
end if
end if
end if
 
The FileSystemObject does not open for "binary" read or write. It will read each byte and tranlate it to the two-byte Unicode equivalent and vice versa for writing but between reading and writing either two Ascii translate to the same Unicode or two Unicode translate to the same Ascii byte or both. Sometimes it works and somtimes "it don't". Some, as revealed in this forum, can read and Response.WriteBinary in ASP many, but not all PDF files. It is the "not all" that kills the plan. Compare Code (Text)
Generate Sort in VB or VBScript
 
Hi John,

Thanx for your response. I search for an other solution for that problem and found the following code , but with the same result .. don`t work . Have you an other possibility ?? To read binary files ?

Function GetFile(FileName)
Dim Stream: Set Stream = CreateObject("ADODB.Stream")
Stream.Type = 1 ' Binary
Stream.Open
Stream.LoadFromFile FileName
GetFile = Stream.Read
Stream.Close
End Function


Thanx a lot
 
Hi,

when I run this code I become back some strength values. e.g. if I opened a file with hex 0x00 0x03 0x05 0x07 .. I become back 0x3F 0x3F 0x3F 0x3F..

The goal is to read a binary file in a var and post the contents to a Java Applet.

If I use the first methode and I read a binary file I become for the values 0x81 ,0x8D, 0x8e, 0x8f, 0x90, 0x9d and 0x9e always the value 0x3F back.

How late is by you ??
 
VB and VBScript operate on a byte array differently. I don't know why.
Code:
    Dim strFile
    strFile = GetFile("C:\AutoExec.bat")
Function GetFile(FileName)
    Dim Stream: Set Stream = CreateObject("ADODB.Stream")
    Dim bytFile
    Dim strHex
    Dim I
    Stream.Type = 1 ' Binary 
    Stream.Open
    Stream.LoadFromFile FileName
    bytFile = Stream.Read
    Stream.Close
    ' TypeName produces Byte() but it is really a byte
    ' array inside a string.
    MsgBox TypeName(bytFile) & " " & LenB(bytFile)
    ' The following works in VB but not VBS
    'For I = 0 To 80
    '    strHex = strHex & Chr(bytFile(I))
    'Next
    ' The following works in VBS.
    For I = 1 To 80
        strHex = strHex & Chr(AscB(MidB(bytFile,I,1)))
    Next
    ' Use AscB(MidB(bytFile,I,1)) to get a Byte type
    Msgbox strHex
        
    GetFile = bytFile
End Function
Compare Code (Text)
Generate Sort in VB or VBScript
 
Hi John,

Many Thanx for your efforts.
I found an other solution for the problem. I convert the binary file in an hex file read this file with the first methode (openTextFile) and convert them in the Java App. back to a binary file. This is not the best solution but this will work fine.

best regards
Axel
 
Similar to this, can any one help me to write routine in vb to read/scan faulty cd and recover contents of the cd.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top