(view source code of b64coder.vbs as plain text)
Option ExplicitDim errResultWScript.Echo "Base64 encoding . . ."
errResult = Base64Encode( "b64coder.vbs", "b64coder.enc" )
If errResult <> 0 Then
ShowError errResult
End If
WScript.Echo "Decoding again . . ."
errResult = Base64Decode( "b64coder.enc", "b64coder.dec" )
If errResult <> 0 Then
ShowError errResult
ElseWScript.Echo "Done." & vbCrLf _
& "Compare the files ""b64coder.vbs"" and " _
& """b64coder.dec"", they should be identical."
End If
Sub ShowError( myError )
On Error Resume Next
Err.Raise myError
WScript.Echo "ERROR " & Err.Number & ": " & Err.Description
Err.Clear
On Error Goto 0
WScript.Quit
End Sub
Function Base64Encode( myFileIn, myFileOut )
' This function uses Belus Technology's XBase64 component to Base64 encode a file.' The XBase64 component is available at' http://www.xstandard.com/page.asp?p=F5096C20-DF8F-4FAA-A1A5-CC85A934139A'' Arguments:' myFileIn [string] the file to be encoded' myFileOut [string] the encoded file to be created'' Return Code:' 0 if all goes well, otherwise the appropriate error number'' Written by Rob van der Woude' http://www.robvanderwoude.com ' Standard housekeeping Dim i, objBase64, objFSO, objFileIn, objFileOut Dim objStreamIn, strCode, strTextConst ForAppending = 8
Const ForReading = 1
Const ForWriting = 2
Const TristateFalse = 0
Const TristateMixed = -2
Const TristateTrue = -1
Const TristateUseDefault = -2
Const otSafeArray = 0
Const otString = 2
' Use custom error handling 'On Error Resume Next ' Open a file system objectSet objFSO = CreateObject( "Scripting.FileSystemObject" )
' Open the input file if it existsIf objFSO.FileExists( myFileIn ) Then
Set objFileIn = objFSO.GetFile( myFileIn )
Set objStreamIn = objFileIn.OpenAsTextStream( ForReading, TriStateFalse )
Else ' Error 53: File not foundBase64Encode = 53
' Close input file and release objectsobjStreamIn.Close
Set objStreamIn = Nothing
Set objFileIn = Nothing
Set objFSO = Nothing
' AbortExit Function
End If
' Create the output file, unless it already existsIf objFSO.FileExists( myFileOut ) Then
' Error 58: File already existsBase64Encode = 58
' Close input file and release objectsobjStreamIn.Close
Set objStreamIn = Nothing
Set objFileIn = Nothing
Set objFSO = Nothing
' AbortExit Function
ElseSet objFileOut = objFSO.CreateTextFile( myFileOut, True, False )
End If
' Read the text from the input file and close the filestrText = objStreamIn.ReadAll( )
objStreamIn.Close
' Base64 encode the text streamSet objBase64 = CreateObject( "XStandard.Base64" )
strCode = objBase64.Encode( strText, otString )
Set objBase64 = Nothing
' Write the result to the output file and close the fileobjFileOut.Write strCode
objFileOut.Close
' Release the objectsSet objStreamIn = Nothing
Set objFileIn = Nothing
Set objFileOut = Nothing
Set objFSO = Nothing
' Return any error codesBase64Encode = Err.Number
On Error Goto 0
End Function
Function Base64Decode( myFileIn, myFileOut )
' This function uses Belus Technology's XBase64 component to Base64 decode a file.' The XBase64 component is available at' http://www.xstandard.com/page.asp?p=F5096C20-DF8F-4FAA-A1A5-CC85A934139A'' Arguments:' myFileIn [string] the file to be decoded' myFileOut [string] the decoded file to be created'' Return Code:' 0 if all goes well, otherwise the appropriate error number'' Written by Rob van der Woude' http://www.robvanderwoude.com ' Standard housekeeping Dim i, objBase64, objFSO, objFileIn, objFileOut Dim objStreamIn, strCode, strTextConst ForAppending = 8
Const ForReading = 1
Const ForWriting = 2
Const TristateFalse = 0
Const TristateMixed = -2
Const TristateTrue = -1
Const TristateUseDefault = -2
Const otSafeArray = 0
Const otString = 2
' Use custom error handling 'On Error Resume Next ' Open a file system objectSet objFSO = CreateObject( "Scripting.FileSystemObject" )
' Open the input file if it existsIf objFSO.FileExists( myFileIn ) Then
Set objFileIn = objFSO.GetFile( myFileIn )
Set objStreamIn = objFileIn.OpenAsTextStream( ForReading, TriStateFalse )
Else ' Error 53: File not foundBase64Decode = 53
' Close input file and release objectsobjStreamIn.Close
Set objStreamIn = Nothing
Set objFileIn = Nothing
Set objFSO = Nothing
' AbortExit Function
End If
' Create the output file, unless it already existsIf objFSO.FileExists( myFileOut ) Then
' Error 58: File already existsBase64Decode = 58
' Close input file and release objectsobjStreamIn.Close
Set objStreamIn = Nothing
Set objFileIn = Nothing
Set objFSO = Nothing
' AbortExit Function
ElseSet objFileOut = objFSO.CreateTextFile( myFileOut, True, False )
End If
' Read the encoded text from the input file and close the filestrCode = objStreamIn.ReadAll( )
objStreamIn.Close
' Base64 decode the text streamSet objBase64 = CreateObject( "XStandard.Base64" )
strText = objBase64.Decode( strCode, otString )
Set objBase64 = Nothing
' Write the result to the output file and close the fileobjFileOut.Write strText
objFileOut.Close
' Release the objectsSet objStreamIn = Nothing
Set objFileIn = Nothing
Set objFileOut = Nothing
Set objFSO = Nothing
' Return any error codesBase64Decode = Err.Number
On Error Goto 0
End Function
page last modified: 2025-10-11; loaded in 0.0092 seconds