【问题标题】:How to run Batch script received as argument on VBscript? [closed]如何运行作为 VBscript 参数接收的批处理脚本? [关闭]
【发布时间】:2017-12-27 14:15:09
【问题描述】:

我将批处理脚本作为第一个参数传递给 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" %*

这就是我回显时的打印方式:

正如我们所见,参数被正确接收。我认为问题在于这一行:

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

【问题讨论】:

    标签: windows batch-file vbscript


    【解决方案1】:

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

    arglist = arglist & " " & EnquoteString(arg)
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-15
      • 2012-08-11
      • 2013-07-30
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多