2

I have batch file & vbs file that runs exe application in hidden mode. Now I would like to open this exe applicatio, but with parameters passed to it.

Batch file:

wscript.exe "C:\~some path~\invisible2.vbs" "C:\~some path~\Rserve_d.exe"

invisible2.vbs:

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

Above code makes sure it runs hidden. But now I need to pass this parameter to the Rserve_d.exe when starting it:

--RS-conf "E:\~some path~\Rconf.cfg"

Please advise. I've tried with adjustments, but it seems, that there is always something wrong in the syntax.

1
  • Have you tried passing it in as a second parameter to invisible2.vbs and just adding Wscript.arguments(1) to the code in the vbs? Commented Jun 1, 2017 at 9:52

2 Answers 2

2

Build the arguments string for your command from the arguments to the script:

Function qq(str)
  qq = """" & str & """"
End Function

args = ""
For i = 1 To WScript.Arguments.Count - 1
  If InStr(WScript.Arguments(i), " ") > 0 Then
    args = " " & qq(WScript.Arguments(i))
  Else
    args = " " & WScript.Arguments(i)
  End If
Next

CreateObject("Wscript.Shell").Run qq(WScript.Arguments(0)) & args, 0, False
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please tell me why we are using """" here? This question may sound silly to you but I am really not able to get it. I tried the similar code(without """") to run a vbs file(displaying the arguments passed) from another vbs file which is turn was executed from cmd. It worked fine for me.
@Kira "" in VBScript is an empty string (the double quotes define a string literal). To put double quotes inside a string literal the nested double quotes must be escaped. In VBScript this is done by doubling the character. Hence """" is the string literal for a single double quote. You could use Chr(34) instead (the Chr() function returns the character for the given ASCII character code, which in case of 34 is a double quote), but that would be more characters. Essentially, the function qq() puts the given string in double quotes.
Thank you so much. This removed my confusion - To put double quotes inside a string literal the nested double quotes must be escaped. In VBScript this is done by doubling the character.
2

Ansgar Wiechers posted his answer before I did so he should deserve the credits. Unfortunately, I had already made the effort of posting an answer as well. To provide some additional functionality to your batch script, you could also check for the return value of the executed VBScript.

Batch file:

setlocal
set "script=c:\~some path~\invisible2.vbs"
set "program=c:\~some path~\rserve_d.exe"
set "params=--RS-conf "e:\~some path~\rconf.cfg""

cscript "%script%" //nologo "%program%" %params%

:: %errorlevel% = 0 - VBScript was executed successfully
:: %errorlevel% = 1 - Missing arguments
:: %errorlevel% = 2 - Shell object creation failed
:: %errorlevel% = 3 - Run method was unable to execute the program

VBScript:

Option Explicit

On Error Resume Next

Dim objShell,_
    strCmdLine,_
    intCount

If (WScript.Arguments.Count < 1) Then
    WScript.Quit(1)
End If

Set objShell = WScript.CreateObject("WScript.Shell")

If (Err.Number <> 0) Then
    WScript.Quit(2)
End If

For intCount = 1 To WScript.Arguments.Count - 1
    strCmdLine = strCmdLine & " " & """" & WScript.Arguments.Item(intCount) & """"
Next

objShell.Run """" & WScript.Arguments.Item(0) & """" & strCmdLine, 0, False

If (Err.Number <> 0) Then
    WScript.Quit(3)
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.