Solved: Pass a variable from VBS to batch

Status
Not open for further replies.

yamiseto

Distinguished
Jul 30, 2010
5
0
18,510
I am working on a batch script for my specific purpose.
The batch has launches a program like so:
[fixed]start /w program.exe %1[/fixed]
This way I can easily pass variable to my batch using a shortcut. Like:
[fixed]C:\mybatch.bat "My variable"[/fixed]
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:
[fixed]CreateObject("Wscript.Shell").Run "mybatch.bat",0[/fixed]
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:
[fixed]C:\start.vbs "My variable"[/fixed]
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.
 
Solution
That's a good "catch all" method, but would be cleaner if you named your arguments rather than using the "blanket" arguments collection containing all args that forces you to use For Each to break it up.

I.E. WScript.Arguments(0) or WScript.Arguments(1)

That's why you were getting an error saying "wrong number of arguments".

In defense of my first post: I always focus on syntax errors first as it's usually the most likely suspect ;)

yamiseto

Distinguished
Jul 30, 2010
5
0
18,510
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:
[fixed]CreateObject("Wscript.Shell").Run "mybatch.bat PARAM1",0[/fixed]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:
[fixed]echo Parameteter = %1> Var.txt[/fixed]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:
[fixed]Parameteter = PARAM1[/fixed]
 

alucardvpr

Distinguished
Apr 22, 2011
4
0
18,520

Yamiseto,

You want to keep your variables out of the quotes like this:

[fixed]CreateObject("Wscript.Shell").Run "mybatch.bat" & PARAM1,0[/fixed]
Hope that helps.
 

yamiseto

Distinguished
Jul 30, 2010
5
0
18,510
Thanks for the reply. I was pretty sure I tried that, 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
[fixed]echo Parameteter = %1> Var.txt[/fixed]start.vbs
[fixed]dim param1
set param1 = wscript.Arguments
CreateObject("Wscript.Shell").Run "test1.bat" & param1,0[/fixed]shortcut.lnk
[fixed]Target: C:\test\start.vbs Testing
Start in: C:\test[/fixed]When I double click shortcut.lnk, I get the following error:
errorer.png


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.
 

alucardvpr

Distinguished
Apr 22, 2011
4
0
18,520
That's a good "catch all" method, but would be cleaner if you named your arguments rather than using the "blanket" arguments collection containing all args that forces you to use For Each to break it up.

I.E. WScript.Arguments(0) or WScript.Arguments(1)

That's why you were getting an error saying "wrong number of arguments".

In defense of my first post: I always focus on syntax errors first as it's usually the most likely suspect ;)
 
Solution
Status
Not open for further replies.