想要使用VBScript在不同的文件夹中运行.bat文件

时间:2016-01-15 22:07:10

标签: batch-file vbscript

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

{
   "took": 10,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "nested_tags": {
         "doc_count": 4,
         "filter_tag_name": {
            "doc_count": 2,
            "tags_name_terms": {
               "doc_count_error_upper_bound": 0,
               "sum_other_doc_count": 0,
               "buckets": [
                  {
                     "key": "key1",
                     "doc_count": 2
                  }
               ]
            },
            "tags_value_terms": {
               "doc_count_error_upper_bound": 0,
               "sum_other_doc_count": 0,
               "buckets": [
                  {
                     "key": "value1",
                     "doc_count": 2
                  }
               ]
            }
         }
      }
   }
}

3 个答案:

答案 0 :(得分:9)

走出困境我会怀疑批处理脚本需要自己的父文件夹作为工作目录。您可以通过将代码更改为此来相应地设置工作目录:

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

如果上述方法无效,则需要提供有关 应该发生什么以及实际 的内容的更多信息。以可见模式运行外部命令/脚本而不自动关闭CMD通常有助于调试:

shell.CurrentDirectory = "C:\Users\js\Desktop\createIndex"
shell.Run "cmd /k createindex.bat", 1, True

答案 1 :(得分:0)

I've 4 simple scripts for what kind of calls.

call.vbs

call_nowait.vbs

call_nowindow.vbs

call_nowindow_nowait.vbs

All the 4 has these lines of code in the beginning:

ReDim args(WScript.Arguments.Count-1)
For i = 0 To WScript.Arguments.Count-1
  If InStr(WScript.Arguments(i), " ") > 0 Then
    args(i) = Chr(34) & WScript.Arguments(i) & Chr(34)
  ElseIf WScript.Arguments(i) = "" Then
    args(i) = Chr(34) & WScript.Arguments(i) & Chr(34)
  Else
    args(i) = WScript.Arguments(i)
  End If
Next

Set objShell = WScript.CreateObject("WScript.Shell")

' MsgBox Join(args, " ")

Plus the last line is different in all 4 scripts:

call.vbs

objShell.Run Join(args, " "), 1, True

call_nowait.vbs

objShell.Run Join(args, " "), 1, False

call_nowindow.vbs

objShell.Run Join(args, " "), 0, True

call_nowindow_nowait.vbs

objShell.Run Join(args, " "), 0, False

For example, the entire command line for the call_nowindow.vbs script looks like this:

call_nowindow.vbs "C:\Users\js\Desktop\createIndex\createindex.bat"

By the way, you can run anything by these scripts, not only just batch files. This one will create an instance of notepad.exe with out window:

call_nowindow.vbs notepad

This one will create cmd console with current date and time, and leave the window open:

call_nowait.vbs cmd /k echo.%DATE% %TIME%

答案 2 :(得分:0)

使用此脚本将目录设置为脚本的当前工作目录。这样,每当脚本更改目录时,它将在那里查找它,而不是将其硬件生根到特定目录。

 scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

这将使脚本中可以使用该目录中正在查找的任何文件。 示例:如果文件位于当前用户桌面上。

C:\ Users \ [用户名] \桌面\ myfile.vbs

将成为:

scriptdir \ myfile.vbs

如果您将其移动到子文件夹"子文件夹"它会保持

SCRIPTDIR \ myfile.vbs

即使脚本位置不同。 " scriptdir"是运行活动脚本的文件夹的占位符。