Results 1 to 8 of 8

Thread: Command Line Arguments

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    31
    I am developing a program that works with files through command lines. (Ex: c:\mprog.exe /function c:\file.unk) -> Would perform "function" on "c:\file.unk". The Command statement returns "/function c:\file.unk" to my program. I was wondering how I might divide the function command from the string. Thanks in advance!

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299

    If you have a space

    in the command$ then just browse through the string using InStr and find the space and from there simply take the rest.
    ie
    Code:
    dim MyCommand as string, Anum as integer
    Anum= InStr(command$," ")
    if Anum <> 0 then
       MyCommand = Mid(command$,Anum + 1)
    end if

  3. #3
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196
    you could use the Split function (if you are using VB6) to parse the string and separate it into items in an array - then you could just look at the first item to see the function, and the second item in the array to get the file name.

    obviously other validating would need to happen also.

    Code:
    'Syntax
    Split(expression[, delimiter[, count[, compare]]])

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    There is one problem with splitting (or InStr) on spaces...

    If you are using a long file name as one of your parameters then you need to take into account the Quotation marks.

    Ie
    Code:
    myprog.exe /function "C:\Program Files\Myfile.typ"
    This would give you the following parameters :

    1. /function
    2. "C:\Program
    3. Files\Myfile.typ"

    So as well as splitting you would also need to perform tests to make sure it isn't inside quotes and concatenating those that are.

  5. #5
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299

    Na the code i put in...

    Would work fine in that case, as it doesn't check for any more spaces. The code that i put in above would do that long filename you wrote no probs, as it gets to the first space and takes the rest as the name. Of course your absolutely right if you try for any more commands or names on the command line.
    BW

  6. #6
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Unfortunately your code would also correctly parse the following line of code :

    Code:
    Myprog.exe ImALittleTeaPot c:\file.unk
    There isn't any checking in there for what exactly /function is (I am assuming it could be something like /o for output file or /e for error log file etc)

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299

    true...

    Thats a good call. But then that wasn't what that code was sposed to do. he could always add
    Code:
    dim MySwitch as string, MyCommand as string, Anum as integer
    Anum= InStr(command$," ")
    if Anum <> 0 then
       MySwitch = Left(command$,anum - 1)
       MyCommand = Mid(command$,Anum + 1)
    end if

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    31

    Smile

    That's great! Thank you! Just what I needed...

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