0

This batch launcher was created so that batch files won't launch a command prompt window... Now that i'm adding more variables to it, it's not working...

I'm launching the VBScript like so;

wscript D:\batchlaunchersyntax.vbs D:\the batch\batch.bat 11 D:\program files\hello world

The last 2 variables change. I would like to launch the batch file passing a number followed by a directory variable.

Can you please help me edit this VBScript in order to do that?

'--------------------8<----------------------
sTitle = "Batch launcher"

Set oArgs = WScript.Arguments
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")

If oArgs.Count <> 3 Then

' Will die after 10 seconds if no one is pressing the OK button
oShell.Popup "Error: You need to supply a file path " _
& "as input parameter!", 10, sTitle, vbCritical + vbSystemModal

Wscript.Quit 1
End If

sFilePath = oArgs(0)
sArg = oArgs(1)

sArg2 = oArgs(2)

If Not oFSO.FileExists(sFilePath) Then
' Will die after 10 seconds if no one is pressing the OK button
oShell.Popup "Error: Batch file not found", _
10, sTitle, vbCritical + vbSystemModal

Wscript.Quit 1
End If

' add quotes around the path in case of spaces

iRC = oShell.Run("""" & sFilePath & """" & sArg & " " & sArg2 & " ", 0, True)

' Return with the same errorlevel as the batch file had
Wscript.Quit iRC

'--------------------8<----------------------
1

Read and follow Syntax : Escape Characters, Delimiters and Quotes:

Using "Double Quotes"

If a single parameter contains spaces, you can still pass it as one item by surrounding in "quotes" - this works well for long filenames.

Applied to your case, use:

wscript D:\batchlaunchersyntax.vbs "D:\the batch\batch.bat" 11 "D:\program files\hello world"

Otherwise, the command line is tokenized as follows:

rem     ↓ script name
wscript D:\batchlaunchersyntax.vbs D:\the batch\batch.bat 11 D:\program files\hello world
rem oArgs(0)    D:\the
rem oArgs(1)    batch\batch.bat
rem oArgs(2)    11
rem oArgs(3)    D:\program
rem oArgs(4)    files\hello
rem oArgs(5)    world
| improve this answer | |
0

Your script only accepts two arguments, so you would have to change this line to reflect the number of arguments you want to pass to the script:

If oArgs.Count <> 2 Then

Change the 2 to whatever your new limit will be.

The first argument, oArgs(0), is the name of the batch script you want to run.

The second argument, oArgs(1), is the name of the parameter to pass to the first argument.

In order to pass more arguments, you must assign oArgs(2) and so on to new variables, and then change the line that reads:

oShell.Run("""" & sFilePath & """" & sArg, 0, True)

...so that more arguments can be passed to the script. You would add those after the sArg variable, thusly: sArg & " " & sArg2 & " " & sArg3 assuming you assigned the passed arguments to sArg2 and sArg3

Of course, your batch file must know what to do with the passed arguments. :-)

Hope this helps!

| improve this answer | |
  • I implemented the changes but I still get: Error: You need to supply a file path as input parameter! when running wscript D:\batchlaunchersyntax.vbs D:\the batch\batch.bat 11 D:\program files\hello world – BlueLostKing Feb 26 '14 at 2:16
  • The order of parameters is important, and it's 0-based index. So, oArgs(0) is the name of the batch file, 1 is the path the batch file is expecting, 2 would be your numeric value. Change your batch logic accordingly. – JSanchez Feb 26 '14 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.