1

From a vbscript I'm trying to get the user profile directory to expand and also run an executable with command line arguments. It is the spaces in the command line args that seem to be causing an issue. I'm close but no matter what I try I can't seem to get it.

This works:

WshShell.Run """%UserProfile%\test.exe"""

What I'm trying to do that does not work:

WshShell.Run """%UserProfile%\test.exe 8.8.8.8 8989 -e cmd.exe"""

I get an error of "The system cannot find the file specified."

Robert
  • 135
  • 2
  • 12
  • 2
    You don't need literal quotes around the whole command just any paths with spaces, try - `WshShell.Run """%UserProfile%\test.exe"" 8.8.8.8 8989 -e cmd.exe"`. It's the equivalent of executing `"%UserProfile%\test.exe" 8.8.8.8 8989 -e cmd.exe` in a command window. – user692942 Jul 15 '20 at 14:49
  • 2
    Thanks a million, man. That works perfectly and exactly what I was looking for. I can mark this as the resolution or you can. – Robert Jul 15 '20 at 14:53

2 Answers2

0

You can debug your variable before any execution with Wscript.Echo or MsgBox

Wscript.Echo chr(34) & CreateObject("wscript.shell").ExpandEnvironmentStrings("%UserProfile%\test.exe") & chr(34) & " 8.8.8.8 8989 -e cmd.exe"
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Why do you insist on `Chr(34)` when `""` will do? Personally this is far more readable `Wscript.Echo """" & CreateObject("wscript.shell").ExpandEnvironmentStrings("%UserProfile%\test.exe") & """ 8.8.8.8 8989 -e cmd.exe"`. – user692942 Jul 15 '20 at 21:50
0

As already mentioned in the comments the reason for the error in the second example is because the literal quotes are only needed to wrap a file path containing spaces, so you line should be;

WshShell.Run """%UserProfile%\test.exe"" 8.8.8.8 8989 -e cmd.exe"

It's the equivalent of writing

"%UserProfile%\test.exe" 8.8.8.8 8989 -e cmd.exe

at the command prompt in a console window (note, the position of the literal quotes).


Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175