1
votes

I get an error when I run this command and I am unsure why.

Running VBScript to execute a bat file, I want to output any error messages to a log file. So to do this I have the code:

Set WshShell =  CreateObject("WScript.Shell")

WshShell.Run "cmd /k ""C:\ProgramTHISSTRING Files (x86)\Folder\File.bat"" > ""C:\Program Files(x86)\Folder\File.txt""", 1, True

(I do have the code to do this more elegantly but for the purpose of the problem, I think this reads better and quicker)

Also I have put in THISSTRING on purpose for the error below.

Once this executes I get this error in CMD

'C:\ProgramTHISSTRING' is not recognized as an internal or external command, operable program or batch file.

I understand it's a space in the file name that has caused the error, however, I have the right quotes according to this stackoverflow question so why is this error happening?

1
Did you not see this from yesterday, still shows on the first page? - user692942
Think you'll need to wrap the whole command passed to cmd /k in quotes so it will be WshShell.Run "cmd /k """"C:\ProgramTHISSTRING Files (x86)\Folder\File.bat"" > ""C:\Program Files(x86)\Folder\File.txt""""", 1, True which will run as "cmd /k ""C:\ProgramTHISSTRING Files (x86)\Folder\File.bat" > "C:\Program Files(x86)\Folder\File.txt"". - user692942

1 Answers

1
votes

I think the issue here is because the cmd /k needs the commands passed to it to be encapsulated in double quotes.

So the command (removed THISSTRING as you said that was just to trigger the error)

WshShell.Run "cmd /k ""C:\Program Files (x86)\Folder\File.bat"" > ""C:\Program Files(x86)\Folder\File.txt""", 1, True

Becomes

WshShell.Run "cmd /k """"C:\Program Files (x86)\Folder\File.bat"" > ""C:\Program Files(x86)\Folder\File.txt""""", 1, True

and run's as

cmd /k ""C:\Program Files (x86)\Folder\File.bat" > "C:\Program Files(x86)\Folder\File.txt""