Batch file 如何使用参数打开exe文件
我有一个批处理文件&vbs文件,它以隐藏模式运行exe应用程序。 现在我想打开这个exe应用程序,但要向它传递参数 批处理文件:Batch file 如何使用参数打开exe文件,batch-file,vbscript,Batch File,Vbscript,我有一个批处理文件&vbs文件,它以隐藏模式运行exe应用程序。 现在我想打开这个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 上面的代码确
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"
请告知。我尝试过调整,但似乎语法总是有问题 从脚本的参数中为命令生成参数字符串:
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
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
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
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
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
选项显式
出错时继续下一步
Dim objShell_
strCmdLine_
intCount
如果(WScript.Arguments.Count<1),则
WScript.Quit(1)
如果结束
设置objShell=WScript.CreateObject(“WScript.Shell”)
如果(错误编号0),则
WScript.Quit(2)
如果结束
对于intCount=1到WScript.Arguments.Count-1
strCmdLine=strCmdLine&&&WScript.Arguments.Item(intCount)和
下一个
objShell.Run“”&WScript.Arguments.Item(0)&“”&strCmdLine,0,False
如果(错误编号0),则
WScript.Quit(3)
如果结束
您是否尝试将其作为第二个参数传递给invisible2.vbs
并将Wscript.arguments(1)
添加到vbs中的代码中?您能告诉我为什么在这里使用“”?这个问题对你来说可能听起来很傻,但我实在搞不懂。我尝试使用类似的代码(不带“”)从另一个vbs文件运行vbs文件(显示传递的参数),该文件是从cmd执行的。对我来说效果很好。@Kira“
在VBScript中是一个空字符串(双引号定义了字符串文字)。若要将双引号放在字符串文字中,必须对嵌套的双引号进行转义。在VBScript中,这是通过将字符加倍来完成的。因此,“
是单个双引号的字符串文字。您可以使用Chr(34)
(函数Chr()
返回给定ASCII字符代码的字符,在34的情况下是双引号),但这将是更多字符。本质上,函数qq()
将给定字符串用双引号括起来。非常感谢。这消除了我的困惑——要在字符串文本中放置双引号,必须对嵌套的双引号进行转义。在VBScript中,这是通过将字符加倍来完成的。