|
-
Aug 22nd, 2000, 10:54 PM
#1
Thread Starter
Junior Member
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!
-
Aug 22nd, 2000, 11:07 PM
#2
Hyperactive Member
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
-
Aug 22nd, 2000, 11:08 PM
#3
Addicted Member
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]]])
-
Aug 22nd, 2000, 11:27 PM
#4
Hyperactive Member
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.
-
Aug 22nd, 2000, 11:37 PM
#5
Hyperactive Member
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
-
Aug 22nd, 2000, 11:46 PM
#6
Hyperactive Member
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)
-
Aug 23rd, 2000, 12:11 AM
#7
Hyperactive Member
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
-
Aug 23rd, 2000, 12:57 AM
#8
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|