如何将变量从VBScript传递到批处理? [英] How to pass variables from VBScript to batch?
问题描述
我正在尝试将参数从VBScript传递到批处理.
I'm trying to pass parameters from VBScript to batch.
这是我的VBScript:
This is my VBScript:
Var1 = "foo"
Var2 = "bar"
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "T.bat "& Var1 & Var2
这是我的批处理文件:
@echo off
echo %1
echo %2
pause>nul
推荐答案
您需要在两个参数之间留一个空格,否则批处理文件将只显示一个参数.
You need a space between the two arguments, otherwise the batch file will see just a single argument.
WshShell.Run "T.bat "& Var1 & Var2
成为
WshShell.Run "T.bat foobar"
为避免无意串联,请更改以下内容:
To avoid the inadvertent concatenation change this:
WshShell.Run "T.bat "& Var1 & Var2
对此:
WshShell.Run "T.bat "& Var1 & " " & Var2
或(更好):
WshShell.Run "T.bat """& Var1 & """ """ & Var2 & """"
其他双引号用于处理参数中的空格.您可以使用引号功能使它更具可读性:
The additional double quotes are for handling spaces in the arguments. You can make that a little more readable by using a quoting function:
Function qq(str)
qq = """" & str & """"
End Function
WshShell.Run "T.bat "& qq(Var1) & " " & qq(Var2)
请注意:在批处理文件中,您可能希望使用%〜1
和%〜2
,而不仅仅是%1
和%2
.代字号从参数中删除外部双引号.
As a side note: in your batch file you may want to use %~1
and %~2
instead of just %1
and %2
. The tilde removes outer double quotes from parameters.
这篇关于如何将变量从VBScript传递到批处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!