Binary Files with wsh/vbs

M. Gallant 07/12/2002

The windows scripting FileSystemObject (FSO) provides good support for reading, and writing text files. However, FSO is not designed to handle writing binary data files. It is often desirable to be able to write binary data to a file directly from a wsh/vbs script. If one has a binary array (say from some other component) in script, then the ADODB.Stream object can be used to write the array directly to a file. In some cases, there is data (such as "binary-packed strings") which can not be easily handled this way. CAPICOM 2 provides a Utilities object which provides some useful conversion functions such as BinaryStringToByteArray which can be used in combination with ADODB.Stream. A Variant Conversion Utility is also available. However, it is desirable to have a simple method/extension to enable writing single byte data to a binary file.

wsh/vbs supports moniker access to Java class files (either built-in system classes, or custom add-on classes). This method can be used on any win32 platform that has the MS JVM installed (Win95, 98, ME, NT4, Win2000 [and XP if JVM is manually installed]). As an example, the FileOutputStream class, wrapped with a BufferedOutputStream, has been wrapped into a very simple utility class FileOutputUtil.java which when compiled, can be used to easily output any byte data to a binary file from a vbs script. This class simply instantiates an instance of the FileOutputStream class, and exposes the close() and write(byte) methods of that class. (read capability can be easily added). Typical performance is ~ 120 kb/sec file writing for 850 MHz Pentium III. The FileOutputUtil compiled class is very small (681 bytes). It can easily be used with any vbs script by placing the class file into the same directory as the vbs script, or into the directory %windir%\Java\Classes making this utility class available for any vbs scripts. The class does NOT need to be registered as COM to be used as a moniker from script.

The following procedure shows one way to use the FileOutputUtil class:

 Sub SaveBinaryJava (FileName, StringBuffer)
  dim oJFile, i
  set oJFile = GetObject("java:FileOutputUtil")
  oJFile.openFile(FileName)
    For i=1 to Lenb(StringBuffer)
    oJFile.writebyte(Ascb(Midb(StringBuffer,I,1)))
    Next
  oJFile.closeFile()
 End Sub

This example can accept a binary-packed string, and outputs the binary data to a file. If a regular vbs String is passed (UNICODE encoded internally), then the file will contain the *UNICODE* byte representation (2 bytes/character). Note that the oJFile.writebyte(bytevalue) method can be used to write ANY binary byte values. The bytevalue is truncated to an 8 bit value for values larger than 255. The CAPICOM script sample SaveBinCert.vbs demonstrates using this class to save a certificate object (encoded as a binary-packed string) to a binary DER certificate file, and compares the java moniker approach described here with an approach using the CAPICOM 2 function Utilities.BinaryStringToByteArray(binString) with ADODB.Stream.