2

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, The quotes must be passed in order for the batch script to receive the two word string as a single parameter. The quotes can be removed from the batch file output by using echo %~1 in the batch scrpt.
1

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. :(

4 Comments

As @Bond says Mike, you need a space... like in my answer :)
sigh should have just gone to bed... :)
Was thinking the same myself... but trying to answer questions is addictive eh? :)
Yeah, I have my days where I'll come in and bang around a bit. I should do it more often (as my rep clearly shows...) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.