Solved Pass a variable from VBS to batch

May 16, 2011 at 16:26:31
Specs: Windows 7
I am working on a batch script for my specific purpose.
The batch has launches a program like so:
start /w program.exe %1

This way I can easily pass variable to my batch using a shortcut. Like:
C:\mybatch.bat "My variable"

Because I the batch waits for the program to do its job, it could be open for extended periods of time. I find it annoying to have a batch constantly open, so looked for a way to hide it and settled on this short piece of VBS:
CreateObject("Wscript.Shell").Run "mybatch.bat",0

This is is exactly what I needed and it makes things a lot easier, with one exception. While using this VBS, I can't pass variables to my batch... or to be more precise don't know how to.
My intention is to make several shortcuts with different variables like so:
C:\start.vbs "My variable"

I want to be able to pass "My variable" to my batch, but still be able to manually launch start.vbs without any variable in a way that won't pass a random variable if it is not defined. Passing a blank variable is fine.
I have tried searching for a solution on Google, but being a total newbie to VBS, I couldn't find the right solution. Any help would be highly appreciated. Thanks in advance.

See More: Pass a variable from VBS to batch

Report •

May 16, 2011 at 20:20:50
I want to add some details about problems that I've run into. For the past four hours or so, since I posted this question, I have been searching for a way to accomplish my task.

I've tried all kinds of different ways of passing and setting a variable into the VBS. My problem, I think, comes when I try to start a batch file. This is what I have been using:

CreateObject("Wscript.Shell").Run "mybatch.bat PARAM1",0
It seems every time I try that, no matter the method I use to pass the variable, the PARAM1 variable doesn't end up being the variable, but just stays PARAM1.

I created a simple batch for the sake of testing my VBS. Batch only has one line, consisting of:

echo Parameteter = %1> Var.txt
The batch writes whatever variable the VBS passes into a TXT. It's an easy way for me to check if the variable has been passes correctly. So, this is the problem I see. No matter what I try, the TXT file says:
Parameteter = PARAM1

Report •

May 17, 2011 at 00:05:46
I'll add more information for clarity. I was told to keep my parameters out of the quotes, which I tried. However that did not solve my problem. I'll try to provide as much info as possible.

In the root of C:\ I have a folder called "test". In that folder are 3 files; test.bat, start.vbs, shortcut.lnk. Here are the contents of each.


echo Parameteter = %1> Var.txt


dim param1
set param1 = wscript.Arguments
CreateObject("Wscript.Shell").Run "test1.bat" & param1,0


Target: C:\test\start.vbs Testing
Start in: C:\test

When I double click shortcut.lnk, I get the following error:

In this case, Var.txt is not even created, so it does not even reach the part where it's supposed to start the batch. I have no clue what is wrong as I am very inexperienced in VBS. Thanks for your help.

Report •

May 17, 2011 at 12:45:37
✔ Best Answer
For Each a In WScript.Arguments
  arg = arg & " " & a
Next 'a
CreateObject("Wscript.Shell").Run "test1.bat" & arg, 1

How To Ask Questions The Smart Way

Report •

Related Solutions

May 17, 2011 at 14:52:56
That works like a charm. That solved my problem. Thank you!

Report •

Ask Question