Returning an errorlevel from WSH VBScripts
Sunday, 3. June 2007, 20:32:29
Most people complain that cscript.exe (Windows Scripting Host) always returns an errorlevel of 0 (zero) to its parent batch process after running WSH scripts, even when the script dies with a known Err.Number. Others complain that their calls to WScript.Quit Err.Number never get called because the script dies before the script gets a chance to call WScript.Quit. And using the horrid On Error Resume Next is simply out of the question here.
So what gives, is there a solution? YES!
The trick is using the information from my previous VBScript post, and strategically handling errors inside the VBScript Class_Terminate() event -- which is guaranteed to be called, even if your script dies during Class_Initialize()
For example:
As a result of saving-off any errors in the final Class_Terminate() event handler, you can adequately return the WSH error code to your parent batch command script for further processing, like so:
...and so forth.
As you can see, this is pretty simple, but most VBScript programmers simply aren't aware of the Class statement introduced in VBScript 5. As such, I rarely ever see it being used out in-the-wild; which is unfortunate.
Hope this helps. Enjoy.
So what gives, is there a solution? YES!
The trick is using the information from my previous VBScript post, and strategically handling errors inside the VBScript Class_Terminate() event -- which is guaranteed to be called, even if your script dies during Class_Initialize()
For example:
Option Explicit
Dim exitcode : exitcode = 0
Class Global
Private Sub Class_Initialize()
s = "" ' Generate an undefined variable error
End Sub
Private Sub Class_Terminate()
exitcode = Err.Number
End Sub
End Class
Dim Main : Set Main = New Global
Set Main = Nothing
WScript.Quit exitcode
As a result of saving-off any errors in the final Class_Terminate() event handler, you can adequately return the WSH error code to your parent batch command script for further processing, like so:
@echo off cscript.exe //NoLogo //E:VBScript test.vbs if errorlevel == 500 echo An undefined variable exists!
...and so forth.
As you can see, this is pretty simple, but most VBScript programmers simply aren't aware of the Class statement introduced in VBScript 5. As such, I rarely ever see it being used out in-the-wild; which is unfortunate.
Hope this helps. Enjoy.

jefftanner # 9. November 2008, 06:22
What do I need to do within a JScript program to flag the calling CScript.exe utility that an error occurred within the program, and thereby CScript.exe then sets the DOS ERRORLEVEL to one (1) ?
I have tried the following, and I cannot get %ERRORLEVEL% to be set to three (3) after using WScript.Quit(3) within either VBScript or JScript files.
Here is my code. What I am doing wrong?
QuitTest.cmd
@ECHO OFF
ECHO.
cscript //NoLogo QuitTest.vbs
ECHO QuitTest.vbs: %errorlevel%
ECHO.
cscript //NoLogo QuitTest.js
ECHO QuitTest.js: %errorlevel%
QuitTest.js
WScript.Echo("JScript QuitTest");WScript.Quit(3);
QuitTest.vbs
WScript.Echo("VBScript QuitTest")WScript.Quit(3)
The results after the following run were that neither the VBScript not the JScript usage of WScript.Quit(3) set the %ERRORLEVEL% to 3.
DOS Run...
D:\Workspace>QuitTest.cmd
VBScript QuitTest
QuitTest.vbs: 0
JScript QuitTest
QuitTest.js: 0
Thanks
Jeff in Seattle
Lee Harvey # 9. November 2008, 13:33
test.js
test.bat
Output
...although, I wouldn't recommend returning 3 as an errorlevel from your WSH scripts. Why? Because 3 is a valid Windows network message number. Therefore, try returning error numbers that do not coincide/clash with valid Windows network message numbers. For example, net helpmsg 3 coincides with message "The system cannot find the path specified" -- which is not the case here.
For testing purposes, -3 might be more appropriate.
HTH
keithel # 2. December 2008, 20:08
I did a rewrite to make this more of a singleton, and requiring no need to store off the exitcode, and no need to add 'WScript.Quit exitcode' at the end of your script...
Without the exclusion of of the odd '-2147155971' Err.Number, explicit WScript.Quit calls within the main body of the script will fail to make the script return the exit code desired -- instead the class terminate will trigger and return the large negative error code above.
Here's my rewrite: