I'm using CScript to run a VBScript file, and I need to pass a command line to the script where the parameter includes both spaces and quotes. The entire command needs to be passed as one parameter to the script. For example:
C:\> CScript myscript.vbs //Nologo "cmd.exe /c "dir && del /q *.txt""
Note the specific command line above is just an example, but it does touch on what I'm trying to do (i.e. pass a command line that involves running cmd.exe). I'm trying to solve for the general case, however.
The issue is that CScript appears to be getting confused trying to determine where quotes are opening and closing. At least sometimes.
The following script may help explain. It just outputs the passed in arguments as it sees them:
Dim count
For count = 0 To WScript.Arguments.Count-1
WScript.Echo CStr(count) & " = " & WScript.Arguments.Item(count)
Next
Often times, it works as expected:
C:\> cscript myscript.vbs //Nologo "1 2 3"
0 = 1 2 3
But when there are quotes inside the quoted parameter, things change:
C:\> cscript myscript.vbs //Nologo "1 "2 3""
0 = 1 2
1 = 3
I understand there is an introduced ambiguity about whether each quote is opening or closing a parameter in the above example, so next I tried escaping the internal quotes so the command processor will treat them as literal strings:
C:\> cscript myscript.vbs //Nologo "1 ^"2 3^""
0 = 1 ^2
1 = 3
Hmmm, looks like that only works when the shell does your globbing and such for you. I doubt cscript understands ^
as an escape character like cmd.exe does.
Using single quotes inside the double-quoted command (e.g. "cmd.exe /c 'dir && cd \'") properly parses as a single parameter but won't work because for the case of invoking cmd.exe with the /c parameter, single quotes are not sufficient. For example:
C:\> cmd /c 'dir && cd \'
''dir' is not recognized as an internal or external command,
operable program or batch file.
I suppose I could just iterate over all the arguments in the script and join them back to one long string, but I'll lose the internal quotes and context that they provide doing that. Or maybe I do use single quotes inside the the double-quoted param and map those back to double quotes inside the script. But what if the intention was single quotes for some future case I'm not considering? Since I'm attempting to solve for the general case, I can't make that determination.
Any ideas?
It appears like you are trying to delineate values in your parameters. I suggest using something besides quotes and adding back the quotes. For example using tildes:
Dim count
For count = 0 To WScript.Arguments.Count-1
WScript.Echo CStr(count) & " = " & replace(WScript.Arguments.Item(count),"~",chr(34))
Next
Passing:
cscript myscript.vbs //Nologo "1 ~2 3~"
0 = 1 "2 3"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With