Solved

Solved: Pass a variable from VBS to batch

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.
6 answers Last reply Best Answer
More about solved pass variable batch
  1. 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
  2. yamiseto said:
    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.

    Yamiseto,

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

    CreateObject("Wscript.Shell").Run "mybatch.bat" & PARAM1,0

    Hope that helps.
  3. 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
    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.
  4. My question was answered on Computing.net by Razor2.3.

    Quote:
    Originally posted by Razor2.3

    For Each a In WScript.Arguments
    arg = arg & " " & a
    Next 'a
    CreateObject("Wscript.Shell").Run "test1.bat" & arg, 1


    Thank you for all your support!
  5. Best answer
    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 ;)
  6. Best answer selected by yamiseto.
Ask a new question

Read More

Programming VBS Apps