Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
MSDN Library

ExitCode Property

Returns the exit code set by a script or program run using the Exec() method.



WshScriptExec Object

Executables set an exit code when they finish running. This conveys the status information when a process ends. Often, it is used to send an error code (or some other piece of information) back to the caller. If the process has not finished, the ExitCode property returns 0. The values returned from ExitCode depend on the application that was called.

The following code shows an example of the ExitCode property.

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
          allInput = allInput & input
          tryCount = 0
     End If

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)
          allInput += input;
          tryCount = 0;

if (oExec. ExitCode!= 0)
     WScript.Echo("Warning: Non-zero exit code");


Applies To:

Community Additions

© 2015 Microsoft