Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to use VBScript to run .bat file in a different folder

I'm trying to run a .bat file using VBScript. I can get the VBScript to work when executed within the same folder as the .bat, however, I can't figure out how to make it successfully run when outside the folder.

Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "C:\Users\js\Desktop\createIndex\createindex.bat"
like image 807
jodies Avatar asked Jan 15 '16 22:01

jodies


People also ask

How do I run a batch file from any location?

bat from anywhere on the command line. Show activity on this post. Create a folder called Batches (lets say in your C drive). Append C:\Batches in your path environment variable and you then can run batch files in that directory from anywhere.


1 Answers

Going out on a limb I would suspect that the batch script requires its own parent folder to be the working directory. You can set the working directory accordingly by changing your code to this:

Set shell = CreateObject("WScript.Shell")
shell.CurrentDirectory = "C:\Users\js\Desktop\createIndex"
shell.Run "createindex.bat"

If the above doesn't help you need to provide more information about what should happen and what actually does happen. Running the external command/script in visible mode and without automatically closing CMD usually helps with debugging:

shell.CurrentDirectory = "C:\Users\js\Desktop\createIndex"
shell.Run "cmd /k createindex.bat", 1, True
like image 200
Ansgar Wiechers Avatar answered Oct 12 '22 03:10

Ansgar Wiechers