然后阅读 WshShell 对象 (CreateObject("Wscript.Shell")) 的 .Run 和 .Exec 方法。请务必注意 .Run 的 bWaitOnReturn 参数和 WshScriptExec 对象的 .Status(和 .Exitcode)属性。 This answer 包含 .Run 和 .Exec 的示例代码。
更新:
a.vbs(不是生产质量代码!):
Option Explicit
Const WshFinished = 1
Dim goWSH : Set goWSH = CreateObject("WScript.Shell")
Dim sCmd, nRet, oExec
sCmd = "cscript .\b.vbs"
WScript.Echo "will .Run", sCmd
nRet = goWSH.Run(sCmd, , True)
WScript.Echo sCmd, "returned", nRet
sCmd = "cscript .\c.vbs"
WScript.Echo "will .Exec", sCmd
Set oExec = goWSH.Exec(sCmd)
Do Until oExec.Status = WshFinished : WScript.Sleep 100 : Loop
WScript.Echo sCmd, "returned", oExec.ExitCode
WScript.Echo "done with both scripts"
WScript.Quit 0
.运行 b.vbs:
MsgBox(WScript.ScriptName)
WScript.Quit 22
和.Execs c.vbs:
MsgBox(WScript.ScriptName)
WScript.Quit 33
输出:
cscript a.vbs
will .Run cscript .\b.vbs
cscript .\b.vbs returned 22
will .Exec cscript .\c.vbs
cscript .\c.vbs returned 33
done with both scripts
MsgBoxes 将证明 a.vbs 等待 b.vbs 和 c.vbs。
更新 II - VBScript 的多处理((c) @DanielCook):
ax.vbs:
Option Explicit
Const WshFinished = 1
Dim goWSH : Set goWSH = CreateObject("WScript.Shell")
' Each cmd holds the command line and (a slot for) the WshScriptExec
Dim aCmds : aCmds = Array( _
Array("cscript .\bx.vbs", Empty) _
, Array("cscript .\cx.vbs", Empty) _
)
Dim nCmd, aCmd
For nCmd = 0 To UBound(aCmds)
' put the WshScriptExec into the (sub) array
Set aCmds(nCmd)(1) = goWSH.Exec(aCmds(nCmd)(0))
Next
Dim bAgain
Do
WScript.Sleep 100
bAgain = False ' assume done (not again!)
For Each aCmd In aCmds
' running process will Or True into bAgain
bAgain = bAgain Or (aCmd(1).Status <> WshFinished)
Next
Loop While bAgain
For Each aCmd In aCmds
WScript.Echo aCmd(0), "returned", aCmd(1).ExitCode
Next
WScript.Echo "done with both scripts"
WScript.Quit 0
.Execs bx.vbs
Do
If vbYes = MsgBox("Tired of this rigmarole?", vbYesNo, WScript.ScriptName) Then Exit Do
WScript.Sleep 300
Loop
WScript.Quit 22
和 cx.vbs:
Do
If vbYes = MsgBox("Tired of this rigmarole?", vbYesNo, WScript.ScriptName) Then Exit Do
WScript.Sleep 500
Loop
WScript.Quit 33
如果没有在错误处理上投入大量精力,请不要在工作中这样做。