Results 1 to 7 of 7

Thread: How do I use multiple command line arguments

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 1999
    Location
    Assen, The Netherlands
    Posts
    4

    Post

    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

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    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]

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 1999
    Location
    Assen, The Netherlands
    Posts
    4

    Post

    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

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    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]

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 1999
    Location
    Assen, The Netherlands
    Posts
    4

    Post

    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

  6. #6
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Post

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 1999
    Location
    Assen, The Netherlands
    Posts
    4

    Post

    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
  •  



Click Here to Expand Forum to Full Width