'************************************************************ ' b64tobin.vbs converts b64 file to bin file ' drag and drop some files or specify by command line. ' ' M. Gallant 07/23/2002 '************************************************************ Option Explicit Const Title = "b64tobin" Const prefix = "_bin_" 'binary output file prefix Const ForReading = 1, ForWriting = 2 Const CAPICOM_LOCAL_MACHINE_STORE = 1 Const CAPICOM_STORE_OPEN_READ_ONLY = 0 Const CAPICOMdnld = "http://www.microsoft.com/downloads/release.asp?ReleaseID=39546" Dim oStore, oUtils, ofso, oFile Dim i, fileargs, filedata, decString, parentpath, binoutfile ' Check syntax. If Wscript.Arguments.Count <1 Then MsgBox "Usage: b64tobin.vbs b64file1 [b64file2] .... ", _ vbInformation, Title WScript.Quit(1) End If If NOT isCapicomAvailable Then MsgBox "CAPICOM is not installed." & vbCrLf & _ "Install capicom first via: " & vbCrLf & _ CAPICOMdnld, vbCritical, Title WScript.Quit(1) End If Set fileargs = WScript.Arguments Set oUtils = CreateObject("CAPICOM.Utilities") Set ofso = CreateObject("Scripting.FileSystemObject") On Error Resume Next 'detect non-base64 files For i = 0 to fileargs.Count -1 'Get all files passed (or drag/dropped). If FileExists(fileargs(i)) Then 'continue for vbs to ignore non-file items Set oFile = ofso.GetFile(fileargs(i)) parentpath = ofso.GetParentFolderName(oFile) & "\" binoutfile = parentpath & prefix & ofso.getBaseName(oFile.Name) LoadFile fileargs(i), filedata 'get file content into string decString = oUtils.Base64Decode(filedata) 'decoded binary-packed string If Err.Number <> 0 Then MsgBox "Could NOT decode the file " & fileargs(i) & vbCrLf & _ "Error: " & Hex(Err.Number) & " " & Err.Description, vbCritical, Title Err.clear Else SaveBinFile binoutfile, oUtils.BinaryStringToByteArray(decString) MsgBox "Decoded and wrote binary file: " & binoutfile, vbInformation, Title End If End If 'FileExists test Next Set oUtils = nothing Set ofso = nothing WScript.Quit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' isCapicomAvailable ' ' Checks if CAPICOM is installed ' Function isCapicomAvailable() Dim oStore On Error Resume Next Set oStore = CreateObject("CAPICOM.Store") oStore.Open CAPICOM_LOCAL_MACHINE_STORE, "Root", CAPICOM_STORE_OPEN_READ_ONLY If Err.Number <> 0 Then isCapicomAvailable = False Exit Function End If isCapicomAvailable = True Set oStore = Nothing On Error GoTo 0 End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' FileExists ' ' Checks if file exists (it not, it might be directory, or shorcut etc..) ' Function FileExists(FileName) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") If Not fso.FileExists(FileName) Then FileExists = False Exit Function End If FileExists = True End Function ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' LoadFile ' ' Read content of FileName and assign to Buffer as string. ' Sub LoadFile (FileName, Buffer) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") If Not fso.FileExists(FileName) Then MsgBox "Error: " & FileName & " file not found." Exit Sub End If Dim ts Set ts = fso.OpenTextFile(FileName, ForReading) Buffer = ts.ReadAll Set fso = nothing End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' SaveBinFile ' ' Save binary array to FileName. ' Sub SaveBinFile (FileName, Binarray) Dim oStream Const adSaveCreateOverWrite = 2 Const adTypeBinary = 1 Const adModeReadWrite = 3 Set oStream = WScript.CreateObject("ADODB.Stream") oStream.type = adTypeBinary oStream.mode = adModeReadWrite oStream.Open oStream.write Binarray oStream.SaveToFile FileName, adSaveCreateOverWrite oStream.Close Set oStream = nothing End Sub