none
CScript.exe NOT setting ERRORLEVEL to 3 if WScript.Quit(3)?

    Question

  • 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 QuitTest.vbs //NoLogo

    ECHO QuitTest.vbs: %errorlevel%

     

    ECHO.

    cscript QuitTest.js //NoLogo

    ECHO QuitTest.js: %errorlevel%

     

     

     

    QuitTest.js

    WScript.Echo("JScript QuitTest");

    WScript.Quit(3);

     

     

     

    QuitTest.vbs

    WScript.Echo("VBScript QuitTest")

    WScript.Quit(3)

     

     

    Result after run, 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

    Sunday, November 09, 2008 4:50 AM

Answers

  • OK

     

    I have found the problem, but it is not what I would have expected with Batch programming.

     

    I had started several Batch files by setting ERRORLEVEL to zero (0)::

     

    ERRORLEVEL problem code:

    SET ERRORLEVEL = 0

    ....

    cscript QuitTest.js //NoLogo

    ECHO %ERRORLEVEL%

     

     

    This was a NO-NO, because when I removed them, the JScripts WScript.Quit(3) worked!!!

     

    Now, why it was a NO-NO, I am not certain yet.

     

    I would have expected that if the JScript quits with a non-zero value, then cscript would have overrided any previous setting of ERRORLEVEL. But this appears not to be the case.

     

    Bye

     

    Jeff in Seattle

     

    Monday, November 10, 2008 3:33 AM