问题描述
我将批处理脚本作为第一个参数传递给 VBScript,但是当我调用它运行时,它没有运行.它什么都不做.没有错误,没有什么.
I am passing a batch script as the first argument to a VBScript, but when I call it to run, it does not run. It does nothing. No error, no nothing.
' 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
这是我在批处理文件中的调用方式:
This is how I call it within my batch file:
wscript .\silent_run.vbs ".\code.bat" "arg 1" "arg2" %*
这是我执行 echo 时的打印方式:
This is how it is printed when I do echo:
正如我们所见,参数被正确接收.我认为问题出在这一行:
As we may see, the arguments are received correctly. I think the problem is this line:
CreateObject("Wscript.Shell").Run arglist, 0, False
如果我删除 ".\code.bat"
作为参数并将其直接放在 VBscript 上,它会正确工作:
If I remove the ".\code.bat"
as argument and put it directly on the VBscript, it works correclty:
wscript .\silent_run.vbs "arg 1" "arg2" %*
...
CreateObject("Wscript.Shell").Run ".\code.bat " & arglist, 0, False
如何接收批处理文件,而不是将其硬编码到 VBScript 中?
How can I receive the also the batch file, instead of hard code it into the VBScript?
注意,我需要让 VBScript 保持静音,即不显示窗口.目前,它已经像这样沉默了:
Notice, I need to VBScript to be silent, i.e. no window showing up. Currently, it is already silent like this due:
推荐答案
如果发现我在字符串开头添加了一个空格:
If figured out that I was adding a white space to the beginning of the string on:
arglist = arglist & " " & EnquoteString(arg)
解析第一个参数时,即处理Batch文件.然后稍后用 Trim()
删除它:
When parsing the first argument, i.e., the Batch file is processed. Then just remove it with Trim()
later:
CreateObject("Wscript.Shell").Run Trim( arglist ), 0, False
这篇关于如何运行作为 VBscript 参数接收的批处理脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!