0

I am passing a batch script as the first argument to a VBScript, but when I call it to run, it does not run. It does nothing. No error, no nothing.

' calling VBScript from another VBScript file passing arguments
' https://stackoverflow.com/questions/17437633/
'
' Check if string contains space
' https://stackoverflow.com/questions/31370456/

Function EnquoteString(argument)
  EnquoteString = Chr(34) & argument & Chr(34)
End Function

arglist = ""
With WScript.Arguments
    For Each arg In.Unnamed
        ' Wscript.Echo "Unnamed: " & arg
        If InStr(arg, " ") > 0 Then
            ' arg contains a space
            arglist = arglist & " " & EnquoteString(arg)
        Else
            arglist = arglist & " " & arg
        End If
    Next
End With
' Wscript.Echo arglist

' Running command line silently with VBScript and getting output?
' https://stackoverflow.com/questions/5690134/
'
' Windows Script Host - Run Method (Windows Script Host)
' http://www.vbsedit.com/html/6f28899c-d653-4555-8a59-49640b0e32ea.asp
'
' Wscript.Echo Wscript.Arguments.Item(0)

' Run a batch file in a completely hidden way
' https://superuser.com/questions/62525/
CreateObject("Wscript.Shell").Run arglist, 0, False

This is how I call it within my batch file:

wscript .\silent_run.vbs ".\code.bat" "arg 1" "arg2" %*

This is how it is printed when I do echo:

enter image description here

As we may see, the arguments are received correctly. I think the problem is this line:

CreateObject("Wscript.Shell").Run arglist, 0, False

If I remove the ".\code.bat" as argument and put it directly on the VBscript, it works correclty:

wscript .\silent_run.vbs "arg 1" "arg2" %*
...
CreateObject("Wscript.Shell").Run ".\code.bat " & arglist, 0, False

How can I receive the also the batch file, instead of hard code it into the VBScript?

Notice, I need to VBScript to be silent, i.e. no window showing up. Currently, it is already silent like this due:

  1. Run a batch file in a completely hidden way
Abdul Karim
  • 4,359
  • 1
  • 40
  • 55
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

1 Answers1

1

If figured out that I was adding a white space to the beginning of the string on:

arglist = arglist & " " & EnquoteString(arg)

When parsing the first argument, i.e., the Batch file is processed. Then just remove it with Trim() later:

 CreateObject("Wscript.Shell").Run Trim( arglist ), 0, False
  1. https://www.w3schools.com/asp/func_trim.asp
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144