Visual Basic Tips / Parsing the command line for parameters

Old DOS based programs often allowed the passing in of command line parameters which could include the name of a file to open upon start, and switches to make the program act differently based upon which switches are specified. Windows programs can take advantage of passing in parameters also. The most common use for them is to pass a path and filename to your application to tell it which file to display upon opening.

The code here will show you how to parse the command line, breaking it into discrete parts and stuffing those parts into an array called Parameters(). You can then use the values in that array to branch to specific code designed to react to those parameters.

To try this code out, start a new project. Add a command button to the form, a text box, and then start a control array with a second text box. The control array of text boxes will be used to display the results of the parsing to you so you can see that it has been done. 8-)

The quick way to make a control array is to place the second text box on the form, then copy and paste another one from it, answering yes to the prompt about creating a new control array. Now you will have Text2(0) and Text2(1) on the form. Since this example dynamically loads more text boxes you will now need to delete Text2(1) or the code will toss an error at you.

For this example you should have a command button, Text1, and Text2(0) on the form when you click the command button to start the test.

Before the program will work we will need a way to pass it some command line parameters.

Fortunately Visual Basic provides a way of specifying them during a test run in the IDE.

Click Tools, Options, and select the Advanced Tab on the resulting dialog shown.

Now enter some parameters in the "Command Line Arguments" entry field as shown, and click OK

Now we're ready for some code. Copy and paste the following sub into the general declarations section of your form.

Here is the listing of the ParseCommandLine sub:




Public Sub ParseCommandLine()

'parse the command line of an application for parameters and stuff an array with them.

'It will handle any number of parameters up to command line maximum length

'holds the positions of Parameter delimiters within command$

Dim Delimiters() As Integer

'holds extracted Parameters

Dim Parameters() As String

'location of each Parameter delimiter found

Dim DelimiterLocation As Integer

'specify the delimiter to use when parsing command$

Dim Delimiter As String

'loop counter

Dim LoopCount As Integer

'number of parameters

Dim NumberParameters As Integer

'length of each Parameter

Dim ParameterLength As Integer

'specify delimiter to use

Delimiter = " "

'display our entire command string

Text1.Text = Command$

'initialize loop counter

LoopCount = 0

'make the Delimiters array one in length

ReDim Preserve Delimiters(LoopCount + 1)

'we're pretty sure the first delimiter is at position one of the command string

Delimiters(LoopCount) = 1

'initialize DelimiterLocation for the loop

DelimiterLocation = 1

'look for delimiters until we don't find anymore

While DelimiterLocation <> 0

'look to see if there is a delimiter in the command string

DelimiterLocation = InStr(DelimiterLocation + 1, Command$, Delimiter, 1)

'give the system time to do other tasks

DoEvents

'if we found a delimiter, record it's location

If DelimiterLocation <> 0 Then

LoopCount = LoopCount + 1

ReDim Preserve Delimiters(LoopCount + 1)

Delimiters(LoopCount) = DelimiterLocation

End If

Wend

'reset the loop counter to zero

LoopCount = 0

'calculate the number of delimiters we've found [also number of Parameters passed in]

NumberParameters = UBound(Delimiters) - 1

'stuff an array with the Parameter values including the delimiting characters

While LoopCount <= NumberParameters

DoEvents

'if this isn't the last delimiters position

If LoopCount <> NumberParameters Then

'calculate the length of this Parameter

ParameterLength = Delimiters(LoopCount + 1) - Delimiters(LoopCount)

Else

'calculate the length of the last Parameter

ParameterLength = Len(Command$) - Delimiters(LoopCount) + 1

End If

'stuff the Parameter in the array

ReDim Preserve Parameters(LoopCount + 1)

Parameters(LoopCount) = Trim(Mid(Command$, Delimiters(LoopCount), ParameterLength))

'test statements for debugging so we can see what's going on

If LoopCount <> 0 Then

'skip the first since it's already loaded

Load Text2(LoopCount)

Text2(LoopCount).Visible = True

Text2(LoopCount).Left = Text2(LoopCount - 1).Left

Text2(LoopCount).Top = Text2(LoopCount - 1).Top + Text2(LoopCount - 1).Height + 50

End If

Text2(LoopCount).Text = Parameters(LoopCount)

'increment the loop counter

LoopCount = LoopCount + 1

Wend

'add code here to branch to specific code based on values of parameters now stuffed in

'the Parameters() array

End Sub

End of listing for ParseCommandLine()

Note that the array is dimmed within the sub, so it's won't be available to other routines. Either put your branching code at the bottom of this sub, or declare the Parameters() array as Public within a module, and remove the dim statement in the above sub to allow other subs to access the Parameters() array values.

Now put this code in the command button's click event so you can give the thing a test.

Private Sub Command1_Click()

ParseCommandLine

End Sub

For actual use you will more than likely want to put the call to ParseCommandLine in your Form Load event, after removing the parts which display values in the text boxes, and load more text boxes. I've colored them red for ease of removal.