|
-
Nov 8th, 1999, 10:19 PM
#1
Thread Starter
New Member
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
-
Nov 8th, 1999, 11:00 PM
#2
You could split the Commandline up yourself, if you have VB6, you can use the Handy Function Split, eg.
Code:
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.
Code:
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
[email protected]
[email protected]
-
Nov 8th, 1999, 11:31 PM
#3
Thread Starter
New Member
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
-
Nov 8th, 1999, 11:40 PM
#4
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
[email protected]
[email protected]
-
Nov 9th, 1999, 04:08 PM
#5
Thread Starter
New Member
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
-
Nov 9th, 1999, 04:18 PM
#6
Frenzied Member
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
[email protected]
A BMW Group Company
-
Nov 10th, 1999, 04:25 PM
#7
Thread Starter
New Member
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 !!!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|