Programming Is Fun
Programming Is Fun

Reputation: 27

Passing a variable from vbscript to batch file

i am very new towards batch programming and vbscripting

i wanted to pass a variable from vbscript to a batch file. here is my vbscript code:

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

Upvotes: 2

Views: 1367

Answers (2)

Mike Devenney
Mike Devenney

Reputation: 1845

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. Putting it outside the quotes and concatenating the strings will pass it to the shell command in the way that you're looking to.

EDIT: forgot the quotes. :(

Upvotes: 1

beercohol
beercohol

Reputation: 2587

You need the command line passed to shell.run to include the contents of your variable a, instead of just the letter "a" itself.

Try this for your 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""". In VB (of all flavours - script, .Net, VB6, etc) you need to put two double-quotes to escape one within a string literal.

The only problem with this is that the double quotes will be passed to your batch file.

Upvotes: 4

Related Questions