Attribute VB_Name = "Process_Command_Line_code" ' Parse_Command_Line_code.bas ' 4-8-99 by Robert Clemenzi ' ' This code processes data from the command-line ' ' The format is - appName /x value /size=data ' where x & size can be any string ' terminated with either space or equal sign ' ' Leading and trailing spaces are removed ' Parameter names are not case sensitive ' All data is stored as strings ' The following are equivalent ' ' appName /i test data /q=more Data ' appName /i test data /q more Data ' '---------------------------------------- ' ' Public Methods ' ' Create_Parm_Array Required, must be called first ' ' Get_Parm_Value(varName$, DefaultValue$) As String ' Returns either the value if it exists ' or it returns the default ' ' Process_Command_Line() Places the command-line parameters in ' the array ' ' Option Explicit Private Parm_Array() As T_Parm_Record Private Type T_Parm_Record ParameterName As String Value As String End Type Public Function Locate_Parm_Record(varName$) As Integer Dim i As Integer, temp$ temp$ = UCase(varName$) For i = 0 To UBound(Parm_Array) If UCase(Parm_Array(i).ParameterName) = temp$ Then Locate_Parm_Record = i Exit Function End If Next i Locate_Parm_Record = -1 ' Null / Not found End Function ' Returns either the value if it exists ' or it returns the default Public Function Get_Parm_Value(varName$, DefaultValue$) As String Dim index As Integer index = Locate_Parm_Record(varName$) If index <> -1 Then Get_Parm_Value = Parm_Array(index).Value$ Else ' Since it doesn't already exist, create an entry Call Set_Parm_Value(varName$, DefaultValue$) Get_Parm_Value = DefaultValue$ End If End Function Public Sub Set_Parm_Value(varName$, Value$) Dim index As Integer index = Locate_Parm_Record(varName$) If index = -1 Then index = UBound(Parm_Array) + 1 ReDim Preserve Parm_Array(index) Parm_Array(index).ParameterName = varName$ End If Parm_Array(index).Value = Value$ End Sub Public Sub Process_Command_Line() Dim FileHandle As Integer Dim TextLine$ Dim CmdLine As String ' Required, must be called first ' Erases all existing parameters ReDim Parm_Array(0) ' The null value has leading and trailing space ' and is never used Parm_Array(0).ParameterName = " Null Parm_Array Value " 'Get command line arguments and test for a null string CmdLine = Trim(Command()) If CmdLine = "" Then Exit Sub Call Parse_Parm_String(CmdLine) End Sub Private Sub Parse_Parm_String(Param$) Dim temp$, ParamName$, Value$ Dim i As Integer, j As Integer, k As Integer If Param$ = "" Then Exit Sub i = InStr(Param$, "/") If i = 0 Then Exit Sub i = i + 1 Do j = InStr(i, Param$, "/") If j = 0 Then ' get the last (or only) paramter temp$ = Trim(Mid(Param$, i)) Else temp$ = Trim(Mid(Param$, i, j - i)) End If ' It is possible for a parameter to contain both space and = ' In that case determine which comes first i = InStr(1, temp$, " ") k = InStr(1, temp$, "=") If i < k And i <> 0 Then k = i If k = 0 Then k = i If k = 0 Then ' /i /k /s ParamName$ = Trim(temp$) Value$ = "Present" Else ' /i data /k=test ParamName$ = Trim(Mid(temp$, 1, k - 1)) Value$ = Trim(Mid(temp$, k + 1)) End If Call Set_Parm_Value(ParamName$, Value$) i = j + 1 Loop Until j = 0 End Sub ' Examples of how to call this Parse_Command_Line_code Private Sub ExampleCalls() Call Process_Command_Line ' Required 1st, this actually creates the array ' temp$ = Get_Parm_Value("dir1", "test data") End Sub