如何用参数打开exe文件

时间:2017-06-01 08:32:30

标签: batch-file vbscript

我有批处理文件&以隐藏模式运行exe应用程序的vbs文件。 现在我想打开这个exe应用程序,但是传递了参数。

批处理文件:

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

invisible2.vbs

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

上面的代码确保它隐藏起来。但是现在我需要在启动时将此参数传递给Rserve_d.exe

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

请指教。我尝试过调整,但似乎语法总是有问题。

2 个答案:

答案 0 :(得分:2)

从脚本的参数构建命令的参数字符串:

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

答案 1 :(得分:1)

在我这样做之前,Ansgar Wiechers发布了他的答案他应该获得学分。不幸的是,我已经努力发布了一个答案。要为批处理脚本提供一些附加功能,还可以检查已执行VBScript的返回值。

批处理文件:

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