Reputation: 4380
I am trying to run the following code in vbscript:
ReturnCode = WshShell.Run("C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe", 0, True)
I get an error when I run this script saying it cannot find the file. I think the problem is spaces in the path, but I don't want to reinstall this application to a different path. How do I get around this?
EDIT: Also, I need to be able to put arguments after the executable. Do the arguments go inside the quotes or outside?
Upvotes: 1
Views: 930
Reputation: 11
I've never been a big fan of multiple quotes appearing in my code, though it is definitely a solution that works.
What I prefer to do to make my code (to me) more readable is to use chr(34) (the ASCII version of the quotation mark) when I'm adding quotes designed to surround a file name or other string that must be encased in quotes. It's more typing, but to me it avoids the potential confusion that a line like """x y"" ""z 1 2""" can cause.
For the example used by the OP, it would look like this:
ReturnCode = WshShell.Run(chr(34) & "C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe" & chr(34), 0, True)
The reason I like this is maybe a little clearer when it comes to a path where you're throwing in command line arguments. For example, when you look at this:
"""C:\Program Files\Some Vendor\Application\program.exe"" -file ""data file.txt"""
It's kind of hard to see what all those quotes are and to figure out what quotes are around what.
Compare that to:
chr(34) & "C:\Program Files\Some Vendor\Application\program.exe" & chr(34) & _
" -file" & chr(34) & "data file.txt" & chr(34)
To my eye, the chr(34) becomes a way to easily visually differentiate between quotation marks that are part of a string definition (e.g., "data file.txt") and those which are needed for Windows to understand the path/filename properly (which show as chr(34) in the example).
If you're consistent about using chr(34) to mean "this is a quotation mark I need in order for Windows to understand the next item in the code" and the normal quotation mark to specify the beginning and ending of a string value, it can even make debugging issues a little easier.
But ultimately each person should do what works for them. My approach takes more typing and might confuse someone who doesn't know what chr() is. The other takes less typing but requires you to do a bit more mental parsing of the string. Neither is really right or wrong.
Upvotes: 1
Reputation: 153
You can get around this by surrounding the path in quotes. But to do so, you need to escape them correctly(with "
), so:
ReturnCode = WshShell.Run("""C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe""", 0, True)
EDIT: Keep the path in double quotes and add around them as necessary:
"""C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe"" argumentGoesHere"
Upvotes: 5
Reputation: 4657
Put the executable inside of double-quotes:
ReturnCode = WshShell.Run( _
"""C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe""", _
0, True)
Upvotes: 1