2
votes

I'm currently executing a batch file using a vbscript to simply hide the cmd window from popping up every time the batch is ran.

What I'm trying to accomplish now is passing an argument to that batch file. The vbs is running with wscript "c:\batchlauncher.vbs" "c:\batch.bat". I'm looking to do something similar to this; wscript "c:\batchlauncher.vbs" "c:\batch.bat" ["batch.argument"] in whatever order or syntax that will accomplish what I need.

This is what I have:

batchlauncher.vbs

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

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

If oArgs.Count <> 1 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)

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 & """", 0, True)

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

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

1 Answers

3
votes

Where are you passing your arguments to the batch file? You need to do this from inside the vbs. Any "extra" arguments the .vbs gets will be in the oArgs array. When you put two arguments to the .vbs file, you need to pass the second set of arguments to the oShell.Run command line.

So I'd change this line
If oArgs.Count <> 1 Then
to this
If oArgs.Count <> 2 Then

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

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