想要使用VBScript在其他文件夹中运行.bat文件

问题描述:

我正在尝试使用VBScript运行.bat文件.在与.bat相同的文件夹中执行该命令时,我可以使VBScript正常工作,但是,我无法弄清楚如何使其在该文件夹之外时成功运行.

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"

不知所措,我怀疑批处理脚本需要其自己的父文件夹作为工作目录.您可以通过将代码更改为以下内容来相应地设置工作目录:

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"


如果上述方法不能解决问题,则您需要提供有关应该发生什么以及实际 发生什么的更多信息.在可见模式下运行外部命令/脚本并且不自动关闭CMD通常有助于调试:


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