Question

I am having issues getting the Run (or Exec) methods to work with VBS. I have been having so much difficulty getting an arbitrary command to run that I have resorted to putting my command in a batch file and trying to use Run to just call the .bat file. Even this method isn't working. Code is as follows:

Dim objShell
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "C:\Path\to\my\batch.bat"
Set objShell = Nothing

Opening a command prompt and simply entering 'C:\Path\to\my\batch.bat' runs exactly as I would expect it to. However when using the VBS code above, it does nothing at all. The command in the batch file is straight forward consisting of the path to the executable and then a path to a file for the executable to act on. Changing the Run command above as follows results in the same thing as trying to call the batch file - nothing at all.

objShell.Run "C:\Path\to\my\executable.exe C:\Path\to\my\file.extension"

My ultimate goal would be to build the path to file string within the script, but that seems irrelevant when even hardcoding it fails. Any help would be much appreciated.

Was it helpful?

Solution

I was able to resolve this with two changes. First, I added "cmd /c" to the front of my command.

This resulted in getting a different error (file not found) and it was actually being caused by timing issues with the program I was trying to execute. Adding a 1 second delay after the Run command (before disposing the objSehll object) resolved this issue.

Thanks to everyone who responded.

OTHER TIPS

Try

objShell.Exec "C:\Path\to\my\batch.bat"

Or

objShell.Run "%comspec% /K C:\Path\to\my\batch.bat"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top