Hi folks,

this time a longer posting, especially the code listing below. Today I’ll show some VBScript sample code on how to read/write a (binary) file and encode/decode it to/from Base64. You may ask why… Okay, this is mainly usefull if you need to call WebServices with byte array parameters when your file lies on disk. BTW: with http://www.pocketsoap.com/ it’s relativly easy to call WebServices with VBScript and I should really blog an example later.

Imagine a WebService to convert certain file types, e.g. image formats. With this sample functions (+ PocketSoap) calling it would be no problem and may turns out usefull for thumbnail generation, picture post editing and whatever you want to do with a binary file received by a WebService on server-side…

Okay, enough blabla, now the script, which is blasting fast btw. This should work out-of-the-box on every XP machine (vista untested), should be self-documenting enough to understand and you may use an image file for proof of concept, like I used a little smiley png. And yes, I named the Script “ReadFileEncodeBase64DecodeBase64WriteFile.vbs” (try typing first letters and press TAB when using cmd) as it’s only used for sample purposes. Happy coding!

Greetz, GHad

01 '
02 ' Call via cmd: cscript ReadFileEncodeBase64DecodeBase64WriteFile.vbs [pathToFile]
03 '
04 ' - Reads file from arg into byte array
05 ' - Encodes byte array to Base64 String
06 ' - Decodes Base64 String to byte array
07 ' - Writes byte array to new file
08 '
09 ' Sample WITHOUT any warrenty! Use at own risk! Copyright 2008 Gerhard Balthasar
10 '
11
12 Option Explicit
13 ' common consts
14 Const TypeBinary = 1
15 Const ForReading = 1, ForWriting = 2, ForAppending = 8
16
17 ' getting file from args (no checks!)
18 Dim arguments, inFile, outFile
19
20 Set arguments = WScript.Arguments
21 inFile = arguments(0)
22 outFile = "new_" & inFile
23
24 Dim inByteArray, base64Encoded, base64Decoded, outByteArray
25  
26 inByteArray = readBytes(inFile)
27 base64Encoded = encodeBase64(inByteArray)
28
29 Wscript.echo "Base64 encoded: " + base64Encoded
30
31 base64Decoded = decodeBase64(base64Encoded)
32 writeBytes outFile, base64Decoded
33
34 Wscript.echo "Finished!"
35
36 private function readBytes(file)
37 dim inStream
38 ' ADODB stream object used
39 set inStream = WScript.CreateObject("ADODB.Stream")
40 ' open with no arguments makes the stream an empty container
41 inStream.Open
42 inStream.type= TypeBinary
43 inStream.LoadFromFile(file)
44 readBytes = inStream.Read()
45 end function
46
47 private function encodeBase64(bytes)
48 dim DM, EL
49 Set DM = CreateObject("Microsoft.XMLDOM")
50 ' Create temporary node with Base64 data type
51 Set EL = DM.createElement("tmp")
52 EL.DataType = "bin.base64"
53 ' Set bytes, get encoded String
54 EL.NodeTypedValue = bytes
55 encodeBase64 = EL.Text
56 end function
57
58 private function decodeBase64(base64)
59 dim DM, EL
60 Set DM = CreateObject("Microsoft.XMLDOM")
61 ' Create temporary node with Base64 data type
62 Set EL = DM.createElement("tmp")
63 EL.DataType = "bin.base64"
64 ' Set encoded String, get bytes
65 EL.Text = base64
66 decodeBase64 = EL.NodeTypedValue
67 end function
68
69 private Sub writeBytes(file, bytes)
70 Dim binaryStream
71 Set binaryStream = CreateObject("ADODB.Stream")
72 binaryStream.Type = TypeBinary
73 'Open the stream and write binary data
74 binaryStream.Open
75 binaryStream.Write bytes
76 'Save binary data to disk
77 binaryStream.SaveToFile file, ForWriting
78 End Sub
Advertisements