Writing Binary Data in WSH - ADODB.Stream for writing binary files
(Page 2 of 4 )
In order to make this work, I first had to determine what a compressed folder looked like in binary. To do that I opened an empty compressed folder with a hex editor. As it turns out, the file is nothing more than a string 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
Binary data is typically written in hexadecimal representation, which consists of two digits ranging from 0 to F. Hexadecimal is a base-16 number system, so each place can have one of sixteen different values. The numbers 0 through 9 are used for the first 10 and then the letters A through F are used for the remaining 6 digits to avoid confusion. In any case, what’s important here is that a byte is two hexadecimal digits.
With CreateObject("ADODB.Stream")
.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 script begins by creating an ADO Stream object and setting its type as text. The reason is that the binary type will only accept a true binary array. This is a special data structure used for manipulating binary files that is not available in VBScript.
Once the stream is opened, we begin writing the binary characters to the stream as you saw earlier. I’m simply supplying each of them in hex notation and using the ChrB function to write a binary character. Characters are written one after another as if we were writing to a standard text stream object.
Next: Fixing the file contents >>
More Code Examples Articles
More By Nilpo