Click to See Complete Forum and Search --> : How do I use multiple command line arguments
menace
Nov 8th, 1999, 09:19 PM
Hi all,
I have a program that reads 3 variables from an INI file. Now I want to get rid of the INI file and get these variables through command line arguments. Just like you could do with BAT files and %1 , %2, %3, etc......
So a users has to enter something like:
PROGRAM.EXE var1 var2 var3
and then the program uses the var1, var2 and var3 to fill other variables.
Does anyone have any idea how to achieve this?
I have been using the COMMAND$ function but it reads al variables as one!!!
If anyone could give any help it would be much appreciated,
Regards,
Dennis ‘t Hart
Aaron Young
Nov 8th, 1999, 10:00 PM
You could split the Commandline up yourself, if you have VB6, you can use the Handy Function Split, eg.
Sub Main()
Dim I As Integer
Dim vCommands As Variant
vCommands = Split(Command, " ")
For I = 0 To UBound(vCommands)
MsgBox "Parameter " & (I + 1) & " = " & vCommands(I)
Next
End Sub
Otherwise you can use the Instr Function, eg.
Sub Main()
Dim I As Integer
Dim sCommands() As String
Dim sLine As String
sLine = Command
While InStr(sLine, " ")
ReDim Preserve sCommands(I)
sCommands(I) = Left(sLine, InStr(sLine, " ") - 1)
I = I + 1
sLine = Mid$(sLine, InStr(sLine, " ") + 1)
Wend
If Len(sLine) Then
ReDim Preserve sCommands(I)
sCommands(I) = sLine
End If
For I = 0 To UBound(sCommands)
MsgBox "Parameter " & (I + 1) & " = " & sCommands(I)
Next
End Sub
------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net
menace
Nov 8th, 1999, 10:31 PM
THANKS !!!! That does do it.... BUT (always a but).
If I enter: PROGRAM.EXE parm1 parm2 parm3
the message box always comes with 1 empty message first for parameter1 and then continues correctly...... any thoughts?
thanks again,
Dennis
Aaron Young
Nov 8th, 1999, 10:40 PM
I can't see why it would, are you pasting this code directly into a Module and using it? If not, maybe you are missing something when splitting up the string. It works fine when I try it.
------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net
menace
Nov 9th, 1999, 03:08 PM
Ok they both work but......
As 1 of the parameters a user can put in is a folder name (with spaces) it doesn't work.
When I change the " " to "/" I get the problem of the first MsgBox being empty....
Cheers
Mark Sreeves
Nov 9th, 1999, 03:18 PM
Surely a path with spaces passed as a parameter should be in quotes.
Most apps require this!
Your command string parsing routine should then check for quotes first and then spaces.
------------------
Mark Sreeves
Analyst Programmer
Mark.Sreeves@Softlab.co.uk
A BMW Group Company
menace
Nov 10th, 1999, 03:25 PM
This did it for me:
Private Sub Form_Load()
Dim xdays, xdrive, xdir
Dim I As Integer
Dim vCommands As Variant
vCommands = Split(Command, "/")
For I = 0 To UBound(vCommands)
If I = 1 Then xdays = vCommands(I)
If I = 2 Then xdrive = vCommands(I)
If I = 3 Then xdir = vCommands(I)
Next I
MsgBox "Number of days=" & xdays & Chr(13) & "Drive and folders=" & Left$(xdrive, 2) & xdir
End
End Sub
THANKS ALL !!!!!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.