0

I'm trying to execute a SAS code with VBScript through the command line. However, I'm having issues with passing the commands from VBScript to command line properly. I'm pretty sure it is a matter of using quotes and chr(34) correctly, but I cannot figure it out.

So I want to run the follow command in command line:

"C:\Program Files\SAS 9.4\sas.exe" -SYSIN "C:\Program Files\test.sas"

I've tried something like

Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /K copy ""C:\Program Files\SAS 9.4\sas.exe"" -SYSIN ""C:\Program Files\test.sas"" ", 1, True
Set oShell = Nothing

But I get an invalid syntax error in command line. Any good ideas?

1 Answer 1

1

CMD /K expects a single argument. If the argument contains spaces, the argument must be quoted. So the full command would be something like:

oShell.run "cmd.exe /K "" ""C:\Program Files\SAS 9.4\sas.exe"" -SYSIN ""C:\Program Files\test.sas"" "" ", 1, True

Normally, one would have to worry about nested quoting/escapes, but the /C and /K switches are special in how quotes work (CMD /? explains the details). If it looks like a single quoted argument, any quotes inside are automatically ignored by CMD.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh yeah I got confused from trying to google. So the command I want to execute is just oShell.run "cmd.exe /K "" ^""C:\Program Files\SAS 9.4\sas.exe^"" -SYSIN ^""C:\Program Files\test.sas^"" "" ", 1, True As this should run the test.sas file in command line. However, the above doesn't work. I get an error with ` '"C:\Program' ` which I think indicates the quoting isn't workin :-/
Ah, my apologies, I got it wrong myself. CMD /K has special behavior regarding quotes, that isn't seen anywhere else in CMD, and which I forgot about. I have corrected the original answer.
Works like a charm. Thank you so much - now my work will be much simpler ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.