简体   繁体   English

将变量从vbscript传递到批处理文件

[英]Passing a variable from vbscript to batch file

i am very new towards batch programming and vbscripting 我对批处理编程和vbscripting非常新

i wanted to pass a variable from vbscript to a batch file. 我想将变量从vbscript传递到批处理文件。 here is my vbscript code: 这是我的vbscript代码:

Dim shell,a

a="Hello World"

set shell=createobject("wscript.shell")
shell.run "test.bat a"

and below here is my batch file: 下面是我的批处理文件:

@Echo off

echo %1

PAUSE

the result i wanted to echo out is "Hello World" but instead "a" is echo out 我想回显的结果是“ Hello World”,但回显了“ a”

You need the command line passed to shell.run to include the contents of your variable a , instead of just the letter "a" itself. 您需要将命令行传递给shell.run以包含变量a内容 ,而不仅仅是字母“ a”本身。

Try this for your VBS: 为您的VBS尝试以下操作:

Dim shell,a

a="Hello World"

set shell=createobject("wscript.shell")
shell.run "test.bat """ & a & """"

That will effectively make the line say shell.run "test.bat ""Hello World""" . 这将有效地使该行显示shell.run "test.bat ""Hello World""" In VB (of all flavours - script, .Net, VB6, etc) you need to put two double-quotes to escape one within a string literal. 在VB(所有类型的脚本,.Net,VB6等)中,您需要放置两个双引号以在字符串文字中转义一个。

The only problem with this is that the double quotes will be passed to your batch file. 唯一的问题是双引号将传递到您的批处理文件。

Try this: 尝试这个:

Dim shell,a

a="Hello World"

set shell=createobject("wscript.shell")
shell.run "test.bat " & """" & a & """"

Your a variable is being included in the filename. a变量已包含在文件名中。 Putting it outside the quotes and concatenating the strings will pass it to the shell command in the way that you're looking to. 将其放在引号之外并连接字符串,将以您希望的方式将其传递给shell命令。

EDIT: forgot the quotes. 编辑:忘记了引号。 :( :(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM