Import Multiple Binary Files to a Script

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Import Multiple Binary Files to a Script

Post by Rain » Thu Aug 08, 2013 3:33 pm

This script imports multiple binary files to a script as native macro scheduler data or with Base64.

1. Run the script and browse to the directory that contains the files you want to import. You can double click on a file in the list box to remove it.

2. Browse to the script to import the binary data to.

3. (Optional)
"Include "ExportData>Data_Label,Filename" for each imported file" adds the string for each file to the end of the selected script.

4. (Optional)
"Include "LabelToVar>LabelName,VariableName" for each imported file" adds the string for each file to the end of the selected script.

5. (Optional)
Check "Import with Base64" to import binary files with Base64 instead. This is much faster and takes up less space. The decode commands are automatically added to the end of the script.
I'm using VBScript because the encoding speed is much faster than the native Base64 command. Plus, the native Base64 command gives me an error with one of my images. (I will report it in a separate topic.)

6. Click "Import Binary Files" to start the process.


Image

Written with Macro Scheduler 13

Code: Select all

Let>APP_TITLE=Multiple Binary Importer

VBSTART
 Function fImportBinary(InputFile,OutputFile)
   Const ForReading = 1, ForWriting = 2, ForAppending = 8, TristateFalse = 0
   Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile(InputFile, ForReading, TristateFalse)
     vFileOut = OutputFile
   Set f2 = fso.OpenTextFile(vFileOut, ForAppending, TristateFalse, True)
     fImportBinary = fImportBinary & vbCrLf & InputFile & " -> " & OutputFile
     LineLength = 0
     Do While Not f.AtEndOfStream
       myByte = hex(asc(f.Read(1)))
       If len(myByte) = 1 Then myByte = "0" & myByte
         vCount2 = vCount2 +1
         f2.write myByte
         If vCount2 mod 256 = 0 Then
         End If
     Loop
   f.close
   f2.close
 End Function
VBEND
//Base64 Encoding
VBSTART
Option Explicit
Function fImportB64Binary(VarInputFile)
  Const adTypeBinary = 1
  Dim objFSO
  Dim objFileOut
  Dim objXML
  Dim objDocElem
  Dim objStream
  Set objStream = CreateObject("ADODB.Stream")
  objStream.Type = adTypeBinary
  objStream.Open()
  objStream.LoadFromFile(VarInputFile)
  Set objXML = CreateObject("MSXml2.DOMDocument")
  Set objDocElem = objXML.createElement("Base64Data")
  objDocElem.dataType = "bin.base64"
  objDocElem.nodeTypedValue = objStream.Read()
  fImportB64Binary = objDocElem.text
  Set objXML = Nothing
  Set objDocElem = Nothing
  Set objStream = Nothing
End Function
VBEND


Dialog>Dialog1
object Dialog1: TForm
  Left = 408
  Top = 81
  HelpContext = 5000
  BorderIcons = [biSystemMenu, biMinimize, biMaximize, biHelp]
  Caption = 'Multiple Binary Importer'
  ClientHeight = 528
  ClientWidth = 426
  Color = clBtnFace
  DefaultMonitor = dmPrimary
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Position = poScreenCenter
  ShowHint = True
  OnTaskBar = True
  PixelsPerInch = 96
  TextHeight = 13
  object MSListBox1: tMSListBox
    Left = 0
    Top = 0
    Width = 426
    Height = 153
    Hint = 'Double click to remove selected'
    Align = alTop
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
    ScrollWidth = 2000
    Sorted = True
    TabOrder = 0
    SelectedIndex = -1
    ExplicitWidth = 470
  end
  object ProgressBar1: TProgressBar
    Left = 0
    Top = 153
    Width = 426
    Height = 17
    Align = alTop
    TabOrder = 1
  end
  object Panel1: TPanel
    Left = 8
    Top = 176
    Width = 409
    Height = 95
    TabOrder = 2
    object Label1: TLabel
      Left = 8
      Top = 9
      Width = 163
      Height = 13
      Caption = 'Directory to import binary data from'
    end
    object Label2: TLabel
      Left = 8
      Top = 49
      Width = 137
      Height = 13
      Caption = 'Script to import binary data to'
    end
    object Edit1: TEdit
      Left = 8
      Top = 25
      Width = 265
      Height = 21
      ReadOnly = True
      TabOrder = 0
    end
    object Edit2: TEdit
      Left = 8
      Top = 65
      Width = 265
      Height = 21
      ReadOnly = True
      TabOrder = 1
    end
    object MSButton1: tMSButton
      Left = 280
      Top = 25
      Width = 75
      Height = 21
      Caption = 'browse...'
      TabOrder = 2
      DoBrowse = False
      BrowseStyle = fbFolder
    end
    object MSButton2: tMSButton
      Left = 280
      Top = 65
      Width = 75
      Height = 21
      Caption = 'browse...'
      TabOrder = 3
      DoBrowse = False
      BrowseStyle = fbFolder
    end
  end
  object Panel2: TPanel
    Left = 8
    Top = 280
    Width = 409
    Height = 81
    TabOrder = 3
    object Label5: TLabel
      Left = 7
      Top = 34
      Width = 153
      Height = 13
      Caption = 'Directory to export binary data to'
    end
    object CheckBox1: TCheckBox
      Left = 7
      Top = 10
      Width = 329
      Height = 25
      Caption = 'Include "ExportData>Data_Label,Filename" for each imported file'
      TabOrder = 0
    end
    object Edit3: TEdit
      Left = 7
      Top = 50
      Width = 265
      Height = 21
      Enabled = False
      ReadOnly = True
      TabOrder = 1
    end
    object MSButton4: tMSButton
      Left = 277
      Top = 50
      Width = 75
      Height = 21
      Caption = 'browse...'
      Enabled = False
      TabOrder = 2
      DoBrowse = False
      BrowseStyle = fbFolder
    end
  end
  object Panel3: TPanel
    Left = 8
    Top = 368
    Width = 409
    Height = 33
    TabOrder = 4
    object CheckBox2: TCheckBox
      Left = 8
      Top = 4
      Width = 353
      Height = 25
      Caption = 
        'Include "LabelToVar>LabelName,VariableName" for each imported fi' +
        'le'
      TabOrder = 0
    end
  end
  object Panel4: TPanel
    Left = 8
    Top = 408
    Width = 409
    Height = 49
    TabOrder = 5
    object Label4: TLabel
      Left = 8
      Top = 6
      Width = 62
      Height = 13
      Caption = 'VBS Timeout'
    end
    object MSComboBox1: tMSComboBox
      Left = 8
      Top = 22
      Width = 113
      Height = 21
      Style = csDropDownList
      ItemIndex = 59
      TabOrder = 0
      Text = '1 Hour'
      Items.Strings = (
        '1 Minute'
        '2 Minutes'
        '3 Minutes'
        '4 Minutes'
        '5 Minutes'
        '6 Minutes'
        '7 Minutes'
        '8 Minutes'
        '9 Minutes'
        '10 Minutes'
        '11 Minutes'
        '12 Minutes'
        '13 Minutes'
        '14 Minutes'
        '15 Minutes'
        '16 Minutes'
        '17 Minutes'
        '18 Minutes'
        '19 Minutes'
        '20 Minutes'
        '21 Minutes'
        '22 Minutes'
        '23 Minutes'
        '24 Minutes'
        '25 Minutes'
        '26 Minutes'
        '27 Minutes'
        '28 Minutes'
        '29 Minutes'
        '30 Minutes'
        '31 Minutes'
        '32 Minutes'
        '33 Minutes'
        '34 Minutes'
        '35 Minutes'
        '36 Minutes'
        '37 Minutes'
        '38 Minutes'
        '39 Minutes'
        '40 Minutes'
        '41 Minutes'
        '42 Minutes'
        '43 Minutes'
        '44 Minutes'
        '45 Minutes'
        '46 Minutes'
        '47 Minutes'
        '48 Minutes'
        '49 Minutes'
        '50 Minutes'
        '51 Minutes'
        '52 Minutes'
        '53 Minutes'
        '54 Minutes'
        '55 Minutes'
        '56 Minutes'
        '57 Minutes'
        '58 Minutes'
        '59 Minutes'
        '1 Hour')
      ListText = 
        '1 Minute'#13#10'2 Minutes'#13#10'3 Minutes'#13#10'4 Minutes'#13#10'5 Minutes'#13#10'6 Minutes'#13 +
        #10'7 Minutes'#13#10'8 Minutes'#13#10'9 Minutes'#13#10'10 Minutes'#13#10'11 Minutes'#13#10'12 Min' +
        'utes'#13#10'13 Minutes'#13#10'14 Minutes'#13#10'15 Minutes'#13#10'16 Minutes'#13#10'17 Minutes' +
        #13#10'18 Minutes'#13#10'19 Minutes'#13#10'20 Minutes'#13#10'21 Minutes'#13#10'22 Minutes'#13#10'23' +
        ' Minutes'#13#10'24 Minutes'#13#10'25 Minutes'#13#10'26 Minutes'#13#10'27 Minutes'#13#10'28 Min' +
        'utes'#13#10'29 Minutes'#13#10'30 Minutes'#13#10'31 Minutes'#13#10'32 Minutes'#13#10'33 Minutes' +
        #13#10'34 Minutes'#13#10'35 Minutes'#13#10'36 Minutes'#13#10'37 Minutes'#13#10'38 Minutes'#13#10'39' +
        ' Minutes'#13#10'40 Minutes'#13#10'41 Minutes'#13#10'42 Minutes'#13#10'43 Minutes'#13#10'44 Min' +
        'utes'#13#10'45 Minutes'#13#10'46 Minutes'#13#10'47 Minutes'#13#10'48 Minutes'#13#10'49 Minutes' +
        #13#10'50 Minutes'#13#10'51 Minutes'#13#10'52 Minutes'#13#10'53 Minutes'#13#10'54 Minutes'#13#10'55' +
        ' Minutes'#13#10'56 Minutes'#13#10'57 Minutes'#13#10'58 Minutes'#13#10'59 Minutes'#13#10'1 Hour' +
        #13#10
    end
  end
  object MSButton3: tMSButton
    Left = 8
    Top = 464
    Width = 145
    Height = 33
    Caption = 'Import Binary Files'
    Default = True
    Enabled = False
    Glyph.Data = {
      DE010000424DDE01000000000000760000002800000024000000120000000100
      0400000000006801000000000000000000001000000000000000000000000000
      80000080000000808000800000008000800080800000C0C0C000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
      3333333333333333333333330000333333333333333333333333F33333333333
      00003333344333333333333333388F3333333333000033334224333333333333
      338338F3333333330000333422224333333333333833338F3333333300003342
      222224333333333383333338F3333333000034222A22224333333338F338F333
      8F33333300003222A3A2224333333338F3838F338F33333300003A2A333A2224
      33333338F83338F338F33333000033A33333A222433333338333338F338F3333
      0000333333333A222433333333333338F338F33300003333333333A222433333
      333333338F338F33000033333333333A222433333333333338F338F300003333
      33333333A222433333333333338F338F00003333333333333A22433333333333
      3338F38F000033333333333333A223333333333333338F830000333333333333
      333A333333333333333338330000333333333333333333333333333333333333
      0000}
    NumGlyphs = 2
    TabOrder = 6
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object StatusBar1: TStatusBar
    Left = 0
    Top = 509
    Width = 426
    Height = 19
    Panels = <>
    SimplePanel = True
  end
  object CheckBox3: TCheckBox
    Left = 176
    Top = 472
    Width = 116
    Height = 17
    Caption = 'Import with Base64'
    TabOrder = 16
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSButton1,Onclick,FilesToImport
AddDialogHandler>Dialog1,MSButton2,Onclick,ScriptToExportTo
AddDialogHandler>Dialog1,MSButton3,Onclick,ImportBinaryFiles
AddDialogHandler>Dialog1,MSListBox1,OnDblClick,RemoveSelected
AddDialogHandler>Dialog1,CheckBox1,OnClick,EnableDisableExportVar
AddDialogHandler>Dialog1,CheckBox3,OnClick,DisablePanelTwoThreeFour
AddDialogHandler>Dialog1,MSButton4,Onclick,ExportDataVarDirectory
AddDialogHandler>Dialog1,,OnClose,ExitScript

SetDialogProperty>Dialog1,MSComboBox1,ItemIndex,4

Show>Dialog1,




SRT>ImportBinaryFiles
  //<--Start un-check CheckBox 1 & 2 if Base64 is checked
  GetDialogProperty>Dialog1,CheckBox3,Checked,Res
  IF>Res=True
  SetDialogProperty>Dialog1,CheckBox1,Checked,False
  SetDialogProperty>Dialog1,CheckBox2,Checked,False
  ENDIF
  //<--End un-check CheckBox 1 & 2 if Base64 is checked

  //<--Start Disable MSButton3
  SetDialogProperty>Dialog1,MSButton3,Enabled,False
  //<--End Disable MSButton3
  
  //<--Start Set VBscript timeout
  GetDialogProperty>Dialog1,MSComboBox1,ItemIndex,TimeoutRes
  Add>TimeoutRes,1
  Let>VBS_TIMEOUT=%TimeoutRes%*60000
  //<--End Set VBscript timeout
  
  //<--Start Import binary files to selcted script
  GetDialogProperty>Dialog1,Edit2,Text,strFileName
  GetDialogProperty>Dialog1,MSListBox1,Text,files
  Separate>files,CRLF,file_names
  SetDialogProperty>Dialog1,ProgressBar1,Position,0
  SetDialogProperty>Dialog1,ProgressBar1,Max,%file_names_count%
  Let>kLine=1
  Let>ExportDataVar=
  Let>vLabelToVariable=
  Let>vDecodeCommands=
  Let>vBaseCode=
  Repeat>kLine
    Let>FileToImport=file_names_%kLine%
    IfFileExists>%FileToImport%
      Let>InFile=file_names_%kLine%
      Let>OutFile=%strFileName%
      ExtractFileName>%InFile%,InFileName
      UpperCase>%InFileName%,InFileNameData
      StringReplace>%InFileNameData%,%SPACE%,_,InFileNameData
      SetDialogProperty>Dialog1,StatusBar1,SimpleText,Importing %InFileName%
      SetDialogProperty>Dialog1,ProgressBar1,Position,%kLine%
      
      GetDialogProperty>Dialog1,CheckBox3,Checked,UseBase64
      IF>UseBase64=True
      
        //Import with Base64
        VBEval>fImportB64Binary("%InFile%"),Base64BinCode
        Let>vBaseCode=%vBaseCode%%CRLF%/*%CRLF%%InFileNameData%_DATA:%CRLF%%Base64BinCode%%CRLF%*/%CRLF%
        WriteLn>%OutFile%,rr,%vBaseCode%
        Let>vDecodeCommands=%vDecodeCommands%%CRLF%%CRLF%LabelToVar>%InFileNameData%_DATA,VAR_%InFileNameData%,0,0,{"*/"}%CRLF%Base64>Var_%InFileNameData%,DECODE,Base64DeRes%CRLF%WriteLn>C:\%InFileName%,r,%Base64DeRes%%CRLF%
        
      ELSE
        //Import as native macro scheduler data
      
        //<--Start Include Export Data if enabled
        GetDialogProperty>Dialog1,CheckBox1,Checked,IncludeExportData
        IF>IncludeExportData=True
          GetDialogProperty>Dialog1,Edit3,Text,ExpDataDir
          Let>ExportDataVar=%ExportDataVar%%CRLF%ExportData>%InFileNameData%_DATA,%ExpDataDir%\%InFileName%
        ENDIF
        //<--End Include Export Data if enabled
      
        //<--Start Include LabelToVar if enabled
        GetDialogProperty>Dialog1,CheckBox2,Checked,IncludeLabelToVariable
        IF>IncludeLabelToVariable=True
          Let>vLabelToVariable=%vLabelToVariable%%CRLF%LabelToVar>%InFileNameData%_DATA,Var_%InFileNameData%,0,0,{"*/"}
        ENDIF
        //<--End Include LabelToVar if enabled
      
        WriteLn>%OutFile%,rr,%CRLF%/*%CRLF%%InFileNameData%_DATA:
        VBEval>fImportBinary("%InFile%", "%OutFile%"),res
        WriteLn>%OutFile%,rr,%CRLF%*/%CRLF%

      ENDIF
    ENDIF
    Add>kLine,1
  Until>kLine=%file_names_count%
  
  //<--Start Add Base64 Decode commad
  GetDialogProperty>Dialog1,CheckBox3,Checked,UseBase64
  IF>UseBase64=True
    Trim>%vDecodeCommands%,vDecodeCommands
    WriteLn>%OutFile%,rr,/*%CRLF%//Base64 Decode Commands%CRLF%Let>IGNOREERRORS=1%CRLF%%vDecodeCommands%%CRLF%*/%CRLF%
  ENDIF
  //<--Start Add Base64 Decode commad
  
  //<--Start Add Export Data to end of script if enabled
  GetDialogProperty>Dialog1,CheckBox1,Checked,IncludeExportData
  IF>IncludeExportData=True
    Trim>%ExportDataVar%,ExportDataVar
    WriteLn>%OutFile%,rr,/*%CRLF%//ExportData Commands%CRLF%%ExportDataVar%%CRLF%*/%CRLF%
  ENDIF
  //<--End Add Export Data to end of script if enabled
  
  //<--Start Add LabelToVar to end of script if enabled
  GetDialogProperty>Dialog1,CheckBox2,Checked,IncludeLabelToVariable
  IF>IncludeLabelToVariable=True
    Trim>%vLabelToVariable%,vLabelToVariable
    WriteLn>%OutFile%,rr,/*%CRLF%//LabelToVar Commands%CRLF%%vLabelToVariable%%CRLF%*/%CRLF%
  ENDIF
  //<--End Add LabelToVar to end of script if enabled
 
  SetDialogProperty>Dialog1,StatusBar1,SimpleText,Finished
  SetDialogProperty>Dialog1,ProgressBar1,Position,%file_names_count%

  //<--Start Enable MSButton3
  SetDialogProperty>Dialog1,MSButton3,Enabled,True
  //<--End Enable MSButton3
END>ImportBinaryFiles


SRT>FilesToImport
  SetDialogProperty>Dialog1,,Visible,False
  SetDialogProperty>Dialog1,MSButton1,BrowseStyle,fbFolder
  SetDialogProperty>Dialog1,MSButton1,DoBrowse,True
  GetDialogProperty>Dialog1,MSButton1,Filename,strFolderName
  ExtractFilePath>%strFolderName%,FileFolderRes
  IF>FileFolderRes<>
    GetFileList>%strFolderName%\*.*,files
    StringReplace>%files%,;,CRLF,file_names
    SetDialogProperty>Dialog1,MSListBox1,Text,%file_names%
    SetDialogProperty>Dialog1,Edit1,Text,strFolderName
    SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,0
    //Enable "Import Binary" Button if File in Edit2 Exists
    GetDialogProperty>Dialog1,Edit2,Text,strFileName
    IfFileExists>%strFileName%
      SetDialogProperty>Dialog1,MSButton3,Enabled,True
    ENDIF
  ENDIF
  SetDialogProperty>Dialog1,,Visible,True
END>FilesToImport


SRT>ScriptToExportTo
  SetDialogProperty>Dialog1,,Visile,False
  SetDialogProperty>Dialog1,MSButton1,Filter,Script File|*.scp
  SetDialogProperty>Dialog1,MSButton1,BrowseStyle,fbOpen
  SetDialogProperty>Dialog1,MSButton1,DoBrowse,True
  GetDialogProperty>Dialog1,MSButton1,Filename,strFileName
  ExtractFileName>%strFileName%,FileNameRes
  IF>FileNameRes<>
    SetDialogProperty>Dialog1,Edit2,Text,strFileName
    //Enable "Import Binary" Button if directory in Edit1 Exists
    GetDialogProperty>Dialog1,Edit1,Text,strFolderName
    IfDirExists>%strFolderName%
      SetDialogProperty>Dialog1,MSButton3,Enabled,True
    ENDIF
  ENDIF
  SetDialogProperty>Dialog1,,Visible,True
END>ScriptToExportTo


SRT>RemoveSelected
  GetDialogProperty>Dialog1,MSListBox1,SelectedItems,FileToRemove
  ExtractFileName%FileToRemove%,TheFileName
  Let>Items=Remove %TheFileName%;Close
  GetCursorPos>X,Y
  PopupMenu>X,Y,Items,res
  IF>res=0
    GetDialogProperty>Dialog1,MSListBox1,Text,FileList
    Separate>FileList,CRLF,FileList_Names
    Let>Counter=0
    Let>NewList=
    Repeat>Counter
      Add>Counter,1
      Let>FileAtLine=FileList_Names_%Counter%
      IF>%FileAtLine%<>%FileToRemove%
        Let>NewList=%NewList%%CRLF%%FileAtLine%
      ENDIF
    Until>Counter,FileList_Names_Count
    Trim>%NewList%,NewList
    SetDialogProperty>Dialog1,MSListBox1,Text,%NewList%
    SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,0
  ENDIF
END>RemoveSelected


SRT>EnableDisableExportVar
  GetDialogProperty>Dialog1,CheckBox1,Checked,Res
  IF>Res=True
    SetDialogProperty>Dialog1,Edit3,Enabled,True
    SetDialogProperty>Dialog1,MSButton4,Enabled,True
  ELSE
    SetDialogProperty>Dialog1,Edit3,Enabled,False
    SetDialogProperty>Dialog1,MSButton4,Enabled,False
  ENDIF
END>EnableDisableExportVar


SRT>ExportDataVarDirectory
  SetDialogProperty>Dialog1,,Visible,False
  SetDialogProperty>Dialog1,MSButton4,BrowseStyle,fbFolder
  SetDialogProperty>Dialog1,MSButton4,DoBrowse,True
  GetDialogProperty>Dialog1,MSButton4,Filename,strDirectoryName
  IfDirExists>%strDirectoryName%
    SetDialogProperty>Dialog1,Edit3,Text,%strDirectoryName%
  ENDIF
  SetDialogProperty>Dialog1,,Visible,True
END>ExportDataVarDirectory


SRT>DisablePanelTwoThreeFour
  GetDialogProperty>Dialog1,CheckBox3,Checked,Res
  IF>Res=True
    SetDialogProperty>Dialog1,CheckBox1,Checked,False
    SetDialogProperty>Dialog1,CheckBox2,Checked,False
    SetDialogProperty>Dialog1,MSComboBox1,Enabled,False
    SetDialogProperty>Dialog1,Panel2,Enabled,False
    SetDialogProperty>Dialog1,Panel3,Enabled,False
    SetDialogProperty>Dialog1,Panel4,Enabled,False
  ELSE
    SetDialogProperty>Dialog1,MSComboBox1,Enabled,True
    SetDialogProperty>Dialog1,Panel2,Enabled,True
    SetDialogProperty>Dialog1,Panel3,Enabled,True
    SetDialogProperty>Dialog1,Panel4,Enabled,True
  ENDIF
END>DisablePanelTwoThreeFour


SRT>ExitScript
  Exit>ExitScript
END>ExitScript


Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts