Mark Deven
Mark Deven

Reputation: 579

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

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?

Upvotes: 1

Views: 1241

Answers (2)

TerrenceSun
TerrenceSun

Reputation: 473

Here is another version that processes all the possible arguments.

Dim strRun
strRun = ""
For Each strArg in WScript.Arguments
    strRun = strRun & """" & strArg & """ " 
Next
CreateObject("Wscript.Shell").Run strRun, 0, False

Excute it as wscript.exe Launch.vbs ProgramIWantToStartInBackground.exe Parameter1 Parameter 2 ... ParameterN.

If there is space in the parameters or path, wrap that with "" as usual.

Upvotes: 1

user692942
user692942

Reputation: 16672

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
    

Upvotes: 1

Related Questions