Robert
Robert

Reputation: 135

System can not find file specified - running executable from vbscript

I'm stuck on writing a VBScript file that can call an executable. Stuck on syntax for double-quotes in string literal.

This line is supposed to correctly write the line that calls the executable:

Print #PayLoadFile, "     WshShell.Run """ & exePath & """ "

exePath is variable holding path to executable, and it is correct.

Trying to get the line above to write to the vbs with the following:

WshShell.Run """C:\Users\John Doe\test.exe"""

When I run VBScript manually editing the file with """ between the executable, I do get correct results.

But instead it writes it as it gets error of System cannot find file specified:

WshShell.Run "C:\John Doe\test.exe" 

Upvotes: 0

Views: 1522

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

In VBScript double quotes inside string literals must be escaped, because the string literal itself must be enclosed in a pair of double quotes. The escaping is done by doubling the double quote. Hence the VBA statement

Print #PayLoadFile, "     WshShell.Run """ & exePath & """ "
'                                      ^^               ^^    these double quotes

creates a VBScript statement with the path in a single set of double quotes:

WshShell.Run "C:\Users\John Doe\test.exe" 
'            ^                          ^                     become these double quotes

To get the required additional two pairs of double quotes in the VBScript file (that will put the path in double quotes for the shell) you need to add 8 more double quotes to the VBA statement:

Print #PayLoadFile, "     WshShell.Run """"""" & exePath & """"""" "
'                                        ^^^^               ^^^^

Upvotes: 1

Related Questions