Binary File, Array Scripting Secrets
(Page 1 of 4 )
It’s time for another installment of Nilpo’s Scripting Secrets. In the third edition of my series featuring insider scripting tips, you’ll learn how to write binary files in two different ways, a useful trick when working with arrays, and how to bring your scripts to life with sounds and spoken text. Today’s article is mostly about having fun!
Writing Binary Files – Method 1 (ADODB.Stream)
I’ve been using the ADODB.Stream object to create binary files in WSH for quite some time now. I discovered this method a while back while trying to find a way to create Compressed Folders natively.
I began by opening a newly-created Compressed Folder in a standard hex editor. I could see that the file was a simple run of twenty-two bytes as follows.
50 4B 05 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
If I could find a way to write those twenty-two bytes to a file, I would be able to create an empty compressed folder by creating its binary contents directly. This proved slightly difficult, because neither VBScript nor WSH provide a means of writing binary information to a file.
strPath = "C:Zip.zip"
Const adTypeBinary = 1
Const adTypeText = 2
Const adWriteChar = 0
Const adSaveCreateNotExist = 1
Const adSaveCreateOverwrite = 2
With CreateObject("ADODB.Stream")
I knew from previous programming experience that I would need an IStream object to write binary data. That's the programming object required to manipulate binary streams. A little research showed that the ADODB.Stream object returned an IStream object, so I began playing around to see if I could get it to do what I wanted.
.Open
.Type = adTypeText
.WriteText ChrB(&h50) & ChrB(&h4B) & ChrB(&h5) & ChrB(&h6)
For i = 1 To 18
.WriteText ChrB(&h0)
Next
.SaveToFile strPath, adSaveCreateNotExist
.Close
The ADODB stream provided two methods for writing files from the stream: one for binary and one for text. Unfortunately, the binary method required a byte array as input. VBScript, not being designed for binary use, does not provide any means of creating a true byte array, so I had to develop a workaround.
I was able to write text to the stream character by character. I used VBScript’s little-known ChrB function to write binary characters to the stream one at a time. Saving the file and closing the stream wrote the binary data to the file as I had hoped.
.Open
.Type = adTypeBinary
.LoadFromFile strPath
.Position = 2
arrBytes = .Read
.Position = 0
.SetEOS
.Write arrBytes
.SaveToFile strPath, adSaveCreateOverwrite
.Close
End With
I thought that I had it figured out, until I tried to open the new compressed folder. Windows politely informed me that the file was corrupt. Examining the new file with the hex editor revealed the problem. The WriteText method had added two unwanted bytes to the beginning of the file before it wrote my binary information. There wasn’t any way to avoid this happening, so I needed a way to remove the unwanted bytes after creating the file.
With the file created, I could change the ADODB Stream type to binary and read in the binary information from my file. I could now manipulate the stream contents and write it back to the file as binary—something I couldn’t do before.
An ADODB stream is similar to a record set in that you can read it a little bit at a time, and a cursor marks your position in the stream. By manually setting the position to 2 and then reading the stream contents, I could read in the binary information I wanted while skipping the first two unwanted bytes.
Then, I returned the cursor position to the beginning of the stream and used the SetEOS method to set a new End Of Stream marker. This effectively clears the contents of the stream before writing back the cleaned-up binary information.
Next: Writing Binary Files with the FileSystemObject >>
More Code Examples Articles
More By Nilpo