# Solved: Pass a variable from VBS to batch



## yamiseto (May 16, 2011)

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.


----------



## Ziggy1 (Jun 18, 2002)

Have you searched what ever program (support site) you are trying to execute to see if they have any command lines that can be written into a batch file.

Is there a reason why you haven't said the name of your program?


----------



## yamiseto (May 16, 2011)

Ziggy1 said:


> Have you searched what ever program (support site) you are trying to execute to see if they have any command lines that can be written into a batch file.
> 
> Is there a reason why you haven't said the name of your program?


My specific purpose really has nothing to do with the program I am using. All I am trying to do is pass variables from a shortcut, through a VBS, to a batch and then to the program. I have used a similar batch for a different program, trying to accomplish a similar task and I have run into the same problem.

Really, the only reason I don't want to mention the program I am using is because I have that part worked out and I do not want to waste community efforts on proving irrelevant advice.


----------



## Ziggy1 (Jun 18, 2002)

I am just trying to get a better idea on what your script does, or the process/purpose. I may not nescesarily be able to answer your question, but may be able to suggest other methods.


----------



## yamiseto (May 16, 2011)

Yes, I realize there are better ways to do this, without having the need to pass variables to a batch, but I prefer to do it in a way that I understand. I understand batch commands, and my batch works fine, I just wanted to add the ability to run it in background, for which I would need to pass variables through a VBS.


----------



## yamiseto (May 16, 2011)

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
```


----------



## Ziggy1 (Jun 18, 2002)

wouldn't it be something like...


```
CreateObject("Wscript.Shell").Run "mybatch.bat" & PARAM1,0
```
you wrapped quote around variable


----------



## yamiseto (May 16, 2011)

Thanks for the reply. I was pretty sure I tried that too, but I went to check anyway. 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.

test.bat

```
echo Parameteter = %1> Var.txt
```
start.vbs

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

```
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.
I have attached test.zip if you would like to see it yourself.


----------



## yamiseto (May 16, 2011)

My question was answered on Computing.net by Razor2.3.



> Originally posted by Razor2.3
> 
> For Each a In WScript.Arguments
> arg = arg & " " & a
> ...


Thank you for all your support!


----------



## Ziggy1 (Jun 18, 2002)

ok that's good, I know VB/VBA but not so much in the VBS,


----------



## yamiseto (May 16, 2011)

Yeah, I realize they have their differences. Also, while I have been searching for a solution, I found another possible method I could try. I could use a program such as Quick Batch File Compiler to compile my script into an executable. It has a background mode option, which lets me avoid using VBS completely. Only down side to that is I can't modify my script on the fly, for use with a different program.

Either way, my problem is solved. Thanks a lot for your support.


----------

