DOS command with quoted argument(s)

es.exe is the command line interface to everything which, apart from DOpus, is my most frequently used application. Both of the following commands work correctly from a DOS window. Output is sent to the clipboard.

"C:\Program Files\everything\es.exe" zipfs.jar|clip
"C:\Program Files\everything\es.exe" path:"\program files" zipfs.jar|clip

When executed from a script, the first command works but the second does not. I also tried triple quotes. Test code and corresponding output as shown below.

var objWShell = new ActiveXObject("WScript.Shell");
var str1 = '"C:\\Program Files\\everything\\es.exe" zipfs.jar';
var str2 = '"C:\\Program Files\\everything\\es.exe" path:"\\program files" zipfs.jar';
var str3 = '"""C:\\Program Files\\everything\\es.exe""" path:"""\\program files""" zipfs.jar';
var cmdstr1 = "cmd.exe /c "+str1+"|clip";
DOpus.output("cmdstr1 = " + cmdstr1);
var res = objWShell.Run(cmdstr1,0,true);
DOpus.output("res = " + res);
var cmdstr2 = "cmd.exe /c "+str2+"|clip";
DOpus.output("cmdstr2 = " + cmdstr2);
var res = objWShell.Run(cmdstr2,0,true);
DOpus.output("res = " + res);
var cmdstr3 = "cmd.exe /c "+str3+"|clip";
DOpus.output("cmdstr3 = " + cmdstr3);
var res = objWShell.Run(cmdstr3,0,true);
DOpus.output("res = " + res);

I appreciate that this is not a DOpus issue but I am hoping that someone knows how to wrangle the WScript.Shell command string to reproduce what works just fine in a DOS window.

Try moving the quotes, "path:\program files" instead of path:"\program files"

Same outcome. :frowning_face:

cmdstr1 = cmd.exe /c "C:\Program Files\everything\es.exe" zipfs.jar|clip
res = 0
cmdstr2 = cmd.exe /c "C:\Program Files\everything\es.exe" "path:\program files" zipfs.jar|clip
res = 255
cmdstr3 = cmd.exe /c """C:\Program Files\everything\es.exe""" """path:\program files""" zipfs.jar|clip
res = 255

You've tried " and """; what about ""?

Do the commands work if you run the full cmd.exe /c ... things from a DOS prompt, without JScript being involved?

Good suggestion. I had not tried with the cmd.exe /c prefix. Results demonstrate that the shell is okay with one quoted string but loses the plot when a second quoted string is introduced. I tried various combinations of single, double and triple quotes to no avail. The DOS shell handling of quoted strings appears to be a black art. I'll see if the likes of Stack Overflow has any insights.

Try this:

cmd.exe /s /c ""C:\Program Files\everything\es.exe" "path:\program files" zipfs.jar|clip"

For testing purposes I removed the |clip so I can see what is or isn't happening.

C:>cmd.exe /s /c ""C:\Program Files\everything\es.exe" zipfs.jar"
C:\Notes\jvm\lib\ext\zipfs.jar
C:\Program Files (x86)\IBM\Lotus\Sametime Connect\jvm\lib\ext\zipfs.jar
C:\Program Files (x86)\Java\jre1.8.0_161\lib\ext\zipfs.jar
C:\Program Files\IBM\BMS\ILC\jre\lib\ext\zipfs.jar

C:>cmd.exe /s /c ""C:\Program Files\everything\es.exe" zipfs.jar path:\program"
C:\Program Files (x86)\IBM\Lotus\Sametime Connect\jvm\lib\ext\zipfs.jar
C:\Program Files (x86)\Java\jre1.8.0_161\lib\ext\zipfs.jar
C:\Program Files\IBM\BMS\ILC\jre\lib\ext\zipfs.jar

C:>cmd.exe /s /c ""C:\Program Files\everything\es.exe" zipfs.jar "path:\program files""

C:>

Not sure I follow sorry.

Eureka! Your suggestion does the trick when I don't stuff up the command string. The command that works is:

cmd.exe /s /c ""C:\Program Files\everything\es.exe" zipfs.jar path:"\program files""
C:\Program Files (x86)\IBM\Lotus\Sametime Connect\jvm\lib\ext\zipfs.jar
C:\Program Files (x86)\Java\jre1.8.0_161\lib\ext\zipfs.jar
C:\Program Files\IBM\BMS\ILC\jre\lib\ext\zipfs.jar

My previous attempt incorrectly quoted "path:\program files" as a single string but path: is a keyword and "\program files" is the associated search string.

Many thanks @jon :+1::grinning:

1 Like

For anyone who is interested, this Stack Overflow thread is useful.

The script here may also be of interest. It runs a command and captures the output into a file, which it then reads in.

(Won't make the quotes any easier, just a related thing.)