user3428422
user3428422

Reputation: 4560

VBScript output shell.run to log file

I know there are a few answers to this already but I am unsure why the answers are not working. I will list what I have done.

Basically I want to log information from a shell.Run command into a log file.

What I have

Dim WshShell
Dim WshProcEnv

Set WshShell =  CreateObject("WScript.Shell")

WshShell.Run """C:\Program Files\Folder\batchFile.bat""", 1, TRUE 

Which successfully runs the batch file.

So looking at answers like this one I have tried the following:

WshShell.Run "cmd /c C:\Program Files\Folder\batchFile.bat > C:\Program Files\Folder\log.txt", 1, TRUE

WshShell.Run "cmd /c ""C:\Program Files\Folder\batchFile.bat > C:\Program Files\Folder\log.txt""", 1, TRUE

WshShell.Run """cmd.exe /c ""C:\Program Files\Folder\batchFile.bat > C:\Program Files\Folder\log.txt""", 1, TRUE

I believe it's something small hence why I've asked a new question.

Upvotes: 1

Views: 5033

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

You need to put quotes around each individual path that contain spaces, not the whole commandline.

WshShell.Run "cmd /c ""C:\Program Files\Folder\batchFile.bat"" > ""C:\Program Files\Folder\log.txt""", 1, True

Usually the best way of handling stuff like this is to put your paths in variables and do the quoting with a user-defined quoting function:

Function qq(str)
  qq = """" & str & """"
End Function

batchfile = "C:\Program Files\Folder\batchFile.bat"
logfile   = "C:\Program Files\Folder\log.txt"

WshShell.Run "cmd /c " & qq(batchfile) & " > " & qq(logfile), 1, True

That greatly helps with keeping your statements readable.

Upvotes: 1

Related Questions