Split command line arguments and quoted file names

Previous topic - Next topic

Kitty Hello

Split path names and arguments apart:
Myprog.exe "this is a test" -O3 -c

Output:
>this is a test<
>-O3<
>-c<


Code (glbasic) Select

LOCAL argv$[], arg$

arg$ = GETCOMMANDLINE$()
SplitPath(arg$, argv$[])

FOR i=0 TO LEN(argv$[])-1
PRINT ">"+argv$[i]+"<", 0,i*16
NEXT
SHOWSCREEN
MOUSEWAIT



FUNCTION SplitPath: path$, spl$[]
LOCAL arg$, ch$, quote
WHILE LEN(path$)
ch$ = MID$(path$,0,1)
path$=MID$(path$,1,8000)

IF ch$="\""
quote=1-quote
CONTINUE
ENDIF

IF ch$=" " OR ch$="\t"
IF quote
arg$=arg$+ch$
ELSE
DIMPUSH spl$[], arg$
arg$=""
ENDIF
CONTINUE
ENDIF
arg$ = arg$+ch$
WEND
IF LEN(arg$) THEN DIMPUSH spl$[], arg$
ENDFUNCTION

Moru

I thought this would be a piece of cake to split the file name from the arguments... but I'm not given any quotes around the filename if there are spaces in it, how can I split this into the correct parts?

c:/test path/program file.exe c:/test path/testfile.txt

Result:
>c:/test<
>path/program<
>file.exe<
>c:/test<
>path/testfile.txt<

Kitty Hello

oh.
you can't call a program that way, can you? At least not from a cmd shell.

Moru

This is from dragging a file onto a glbasic program, windows is supposed to add those quotes I thought? Why does it need to pass the exe file anyway, why not just the parameters?

Kitty Hello

You're right. Or I should quote them automatically. I'll see.

Moru

Never mind, works as intended in v7 for some reason :-) Good job!