返回由通过 Exec() 方法运行的脚本或程序设置的退出代码。
Object.ExitCode
可执行程序在完成运行时设置退出代码。它在过程结束时传递状态信息。通常,它用于将错误代码(或某些其他信息段)发送回调用方。如果过程仍未完成,ExitCode 属性返回 0。ExitCode 返回的值取决于所调用的应用程序。
下面的代码示例 ExitCode 属性。
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("%comspec% /c dire")
Function ReadAllFromAny(oExec)
If Not oExec.StdOut.AtEndOfStream Then
ReadAllFromAny = oExec.StdOut.ReadAll
Exit Function
End If
If Not oExec.StdErr.AtEndOfStream Then
ReadAllFromAny = oExec.StdErr.ReadAll
Exit Function
End If
ReadAllFromAny = -1
End Function
Dim allInput, tryCount
allInput = ""
tryCount = 0
Do While True
Dim input
input = ReadAllFromAny(oExec)
If -1 = input Then
If tryCount > 10 And oExec.Status = 1 Then
Exit Do
End If
tryCount = tryCount + 1
WScript.Sleep 100
Else
allInput = allInput & input
tryCount = 0
End If
Loop
If oExec.ExitCode
<> 0 Then
WScript.Echo "Warning: Non-zero exit code"
End If
WScript.Echo allInput
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("%comspec% /c dire");
function ReadAllFromAny(oExec)
{
if (!oExec.StdOut.AtEndOfStream)
return oExec.StdOut.ReadAll();
if (!oExec.StdErr.AtEndOfStream)
return oExec.StdErr.ReadAll();
return -1;
}
var allInput = "";
var tryCount = 0;
while (true)
{
var input = ReadAllFromAny(oExec);
if (-1 == input)
{
if (tryCount++ > 10 && oExec.Status == 1)
break;
WScript.Sleep(100);
}
else
{
allInput += input;
tryCount = 0;
}
}
if (oExec.ExitCode
!= 0)
{
WScript.Echo("Warning: Non-zero exit code");
}
WScript.Echo(allInput);