-
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!
-
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
-
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]]])
-
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.
-
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 :cool:
-
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)
-
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
-
That's great! Thank you! Just what I needed...