can vbscript read BINARY files? 
Author Message
 can vbscript read BINARY files?

you can use the filesystem object to read/write text files, but can VBScript
in any way read/write binary files?
in VB, to read 5 bytes from a binary file, id use...
    MyString = String(5,0)
    Open "filename.bin" For Binary As #1
     Get #1, 1, MyString
    Close #1

is it possible to do something similar in VBScript ?

Many thanks in advance



Sun, 29 Dec 2002 03:00:00 GMT  
 can vbscript read BINARY files?

Quote:

> you can use the filesystem object to read/write text files, but can
> VBScript in any way read/write binary files?
> in VB, to read 5 bytes from a binary file, id use...
>     MyString = String(5,0)
>     Open "filename.bin" For Binary As #1
>      Get #1, 1, MyString
>     Close #1

> is it possible to do something similar in VBScript ?

> Many thanks in advance

In general, the answer is no.  For example, the random read shown in
your example cannot be done nor simulated in VBScript.  All reads are
sequential and require EOL or EOF terminators.  That is, you cannot read
the first five bytes of any file, unless they represent either the
entire first line of the file or the entire file.  But, depending upon
what you are trying to accomplish it may be possible to 'interpret' the
data read by the text methods in a useful way.  

Since the text files need either line delimiters or the end-of-file, a
binary file is best read into a buffer string with a ReadAll.  Then the
bytes can be parse out of the buffer.  Thus, my best guess at
recomposing your code into a VBScript is ...

  Dim MyString(4)
  Set oFS = CreateObject ("Scripting.FileSystemObject")
  Set oInFile = oFS.OpenTextFile(sInfileName, 1)
  sBuffer = oInfile.ReadAll
  For I = 0 to 4
    MyString(I) = ChrB(Asc(Mid(sBuffer, I + 1, 1)))
  Next
  oInFile.Close

Of course, the MyString array is an array of type Variant (and not a
byte array), since scripting can only create/work with Variants.

I don't know whether that will serve your purposes, but it's the best I
can do.

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/



Sun, 29 Dec 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Reading binary files in VBScript

2. Reading binary files in VBScript

3. Reading binary file using VBScript

4. reading binary file in vbscript

5. Reading Binary File through VBScript

6. Read Binary data in VBscript.

7. open a binary file for reading/writing

8. Reading in a binary file...

9. Reading binary file

10. How to read binary files?

11. Reading binary data in a postscript file

12. How can I read binary file???

 

 
Powered by phpBB® Forum Software