如何在VBscript上运行作为参数接收的Batch脚本?

时间:2017-07-21 13:38:14

标签: windows batch-file vbscript

我将批处理脚本作为VBScript的第一个参数传递,但是当我调用它运行时,它不会运行。它什么都不做。没有错误,没有任何错误。

' calling VBScript from another VBScript file passing arguments
' https://stackoverflow.com/questions/17437633/
'
' Check if string contains space
' https://stackoverflow.com/questions/31370456/

Function EnquoteString(argument)
  EnquoteString = Chr(34) & argument & Chr(34)
End Function

arglist = ""
With WScript.Arguments
    For Each arg In.Unnamed
        ' Wscript.Echo "Unnamed: " & arg
        If InStr(arg, " ") > 0 Then
            ' arg contains a space
            arglist = arglist & " " & EnquoteString(arg)
        Else
            arglist = arglist & " " & arg
        End If
    Next
End With
' Wscript.Echo arglist

' Running command line silently with VBScript and getting output?
' https://stackoverflow.com/questions/5690134/
'
' Windows Script Host - Run Method (Windows Script Host)
' http://www.vbsedit.com/html/6f28899c-d653-4555-8a59-49640b0e32ea.asp
'
' Wscript.Echo Wscript.Arguments.Item(0)

' Run a batch file in a completely hidden way
' https://superuser.com/questions/62525/
CreateObject("Wscript.Shell").Run arglist, 0, False

这就是我在批处理文件中调用的方式:

wscript .\silent_run.vbs ".\code.bat" "arg 1" "arg2" %*

这是我回音时打印的方式:

enter image description here

正如我们所看到的,正确接收了这些参数。我认为问题在于这一行:

CreateObject("Wscript.Shell").Run arglist, 0, False

如果我删除".\code.bat"作为参数并将其直接放在VBscript上,它的工作原理是正确的:

wscript .\silent_run.vbs "arg 1" "arg2" %*
...
CreateObject("Wscript.Shell").Run ".\code.bat " & arglist, 0, False

如何收到批处理文件,而不是硬编码到VBScript中?

注意,我需要VBScript保持沉默,即没有窗口出现。目前,它已经像这样保持沉默:

  1. Run a batch file in a completely hidden way

1 个答案:

答案 0 :(得分:1)

如果发现我在字符串的开头添加了一个空格:

arglist = arglist & " " & EnquoteString(arg)

解析第一个参数时,即处理批处理文件。然后,稍后使用Trim()将其删除:

 CreateObject("Wscript.Shell").Run Trim( arglist ), 0, False
  1. https://www.w3schools.com/asp/func_trim.asp