General discussion

Locked

Command line switches as variables

By jcaravella2 ·
I am working on a project and would like to input a couple of switches for my
app on start how do I code the variables in VB6 normally in vbs I can use the
following:

Set InputItems=Wscript.Arguments
name1 = InputItems.item(0)
name2 = InputItems.item(1)

But I don?t know how to do the same with a vb6 executable.

What I want to do is read a switch provided when I execute my code and execute
different sequence of events (different set of code)once the program is
executed.

For instance, take the ping command, it will take either a machine name or an IP
as input - this is what I want to do. Any examples would be appreciated.

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Command line switches as variables

by jcaravella2 In reply to Command line switches as ...

Point value changed by question poster.

Collapse -

Command line switches as variables

by sl-campbell In reply to Command line switches as ...

The VB Command() function returns a string of all parameters assigned to the command line. First, you need to assign a string variable to it:
sCommandLineInput = Command()
sCommandLineInput=Trim$(sCommandLineInput)

There is an example available in then VB Help for the Command() function.

You need to create a function that will parse and return an array of the parameters passed (characters in the variable "sCommandLineInput").
How complicated the function should be, is up to you and what you define as a switch (i.e. does the switch have to have a "/", or some other character before it, or nothing, or are spaces needed between the switches, or not, or ALL of the above conditions in determining if a character is a switch or not possible?). If there are many conditions to determine if a character is a switch then you will need to loop through each character, look if a switch indicator is used or not, get the actual parameter and assign it to an array.

Because I am limited in the number of characters that I can have in an answer, I will post a simple solution in an attached comment below.

Collapse -

Command line switches as variables

by sl-campbell In reply to Command line switches as ...

SIMPLE SOLUTION:

If you want to keep it simple and easy then you can do this:

'1. Make sure a switch indicator has to be used in order to determine a parameter (such as a "/")

'2. Get the command line and remove any spaces:

Const cSWITCH$ = "/"

Dim sCommandLineInput As String

'Get the Command Line input
sCommandLineInput = Command()
sCommandLineInput = Trim$(sCommandLineInput)

'3. Next, for this simple example, need to make sure that all parameters have a proper switch indicator (i.e. "/")

'If the first item doesn't have a switch indicator, then add one
If Left$(sCommandLineInput, 1) <> cSWITCH Then sCommandLineInput = cSWITCH & sCommandLineInput

'Replace All spaces with a switch indicator (i.e. "/")
sCommandLineInput = Replace(sCommandLineInput, " ", cSWITCH)

'3. Create an array using the Split function:

Dim vInputItems As Variant
vInputItems = Split(sCommandLineInput, cSWITCH)

'4. Now you can inspect each item

Dim vItem As Variant

For Each vItem In vInputItems

'Check if vItem is a valid switch that you want to use

If Len(vItem) <> 0 Then
Select Case vItem
Case "A"
'Do something for this parameter type
Case "B"
'Do something for this parameter type
Case "?"
'HELP - Show help text for command line input
Case Else
'Not a Valid parameter -ignor

End Select
End If

'Get the next parameter passed
Next vItem

'(Please note that I typed this out of my head and didn't check for syntax errors but you should get the idea here)

Collapse -

Command line switches as variables

by jcaravella2 In reply to Command line switches as ...

Outstanding, thank you for the help.

Collapse -

Command line switches as variables

by jcaravella2 In reply to Command line switches as ...

This question was closed by the author

Back to Web Development Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums