computing
  • 34

Solved Pass a variable from VBS to batch

  • 34

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.

Share

1 Answer

  1. 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

    • 0