GLBasic forum

Codesnippets => Code Snippets => Topic started by: Kitty Hello on 2008-Jul-03

Title: Split command line arguments and quoted file names
Post by: Kitty Hello on 2008-Jul-03
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
Title: Re: Split command line arguments and quoted file names
Post by: Moru on 2009-Jul-19
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<
Title: Re: Split command line arguments and quoted file names
Post by: Kitty Hello on 2009-Jul-20
oh.
you can't call a program that way, can you? At least not from a cmd shell.
Title: Re: Split command line arguments and quoted file names
Post by: Moru on 2009-Jul-20
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?
Title: Re: Split command line arguments and quoted file names
Post by: Kitty Hello on 2009-Jul-20
You're right. Or I should quote them automatically. I'll see.
Title: Re: Split command line arguments and quoted file names
Post by: Moru on 2009-Jul-20
Never mind, works as intended in v7 for some reason :-) Good job!