Home > other >  How do I make this vbs script accept parameters when launching a file in the background?
How do I make this vbs script accept parameters when launching a file in the background?

Time:10-12

I have the following in a VBS file called "Launch.vbs":

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

I can call this from a batch file like this:

wscript.exe "Launch.vbs" "ProgramIWantToStartInBackground.exe"

However, this does not allow me to pass parameters to it, for example:

wscript.exe "Launch.vbs" "ProgramIWantToStartInBackground.exe" "Parameter"
wscript.exe "Launch.vbs" "ProgramIWantToStartInBackground.exe" Parameter

In both of the above examples, the program does launch in the background, but does not receive any parameters given.

How can I modify the vbscript code to allow a parameter?

CodePudding user response:

There are lots of ways of tackling this problem, but it boils down to understanding how programs run from the command prompt.

This line in launch.vbs is going to wrap any command passed in double-quotes which is fine for just the executable but causes issues when parameters are passed, as only the executable path should be wrapped in double-quotes to avoid issues with any spaces that might be in the path.

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

Instead, you have two options.

  1. Pass two arguments into launch.vbs and only wrap the first one in double quotes.

    CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """ " & 
    WScript.Arguments(1), 0, False
    

    Exeute using;

    wscript.exe "Launch.vbs" "ProgramIWantToStartInBackground.exe" Parameter
    

    Executes as;

    "ProgramIWantToStartInBackground" Parameter
    
  2. Remove the wrapping and apply to the argument passed in.

    CreateObject("Wscript.Shell").Run WScript.Arguments(0), 0, False
    

    Execute using;

    wscript.exe "Launch.vbs" ""ProgramIWantToStartInBackground.exe" Parameter"
    

    Executes as;

    "ProgramIWantToStartInBackground" Parameter
    
  • Related