Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WSH (CSCRIPT.EXE) sets ERRORLEVEL to ZERO for un-trapped errors

771 views
Skip to first unread message

Wayne Erfling

unread,
Nov 7, 2001, 11:58:06 AM11/7/01
to
It is very difficult to use CSCRIPT.EXE in command files, because CSCRIPT.EXE sets ERRORLEVEL to ZERO for both script syntax errors and for un-trapped object errors (for example, ADO errors).
 
Therefore, when calling a script with code such as the following:
 
...
if (HaveError)
   WScript.quit(62);
...
// normal end of script; no error
 
with a command file such as the following:
 
CSCRIPT.EXE myscriptfile.js
IF ERRORLEVEL 1 GOTO PROCESSERROR
REM assume script must have succeeded
 
there are potentially many cases where ERROR will be equal to ZERO, even though the script has in fact failed.
 
I urge Microsoft to fix CSCRIPT.EXE, so that ERRORLEVEL of zero will only be returned when WScript.quit(n) is called with n=0, or when the script comes to a normal conclusion with no errors.
 
---Wayne Erfling (Compaq Computer)

Michael Harris (MVP)

unread,
Nov 7, 2001, 9:07:31 PM11/7/01
to
Well, seems you're right. My recollection is that in earlier WSH versions (at least pre-5.6), unhandled runtime errors (at least under cscript) would always return 1.

Here's my simple repro BAT file (also CC'd to Mike Whalen)...

=====================================

@echo wscript.quit wscript.arguments(0) > test.vbs

cscript.exe test.vbs 99
@echo errorlevel = %ERRORLEVEL%
cscript.exe test.vbs
@echo errorlevel = %ERRORLEVEL%

del test.vbs

=====================================

--
Michael Harris
Microsoft.MVP.Scripting


"Wayne Erfling" <wayne....@compaq.com> wrote in message news:O9won16ZBHA.1752@tkmsftngp07...

Charles Long

unread,
Nov 8, 2001, 4:20:49 AM11/8/01
to
Just to demonstrate the inconsistency of which Wayne
complains, if the first line of Michaels's script is
amended as follows:

@echo wscript.echo(wscript.arguments(0)) wscript.quit
(wscript.arguments(0)) > test.vbs

then the following output ensues at runtime:

C:\scripts\SCRIPT~1\gradings\test.vbs(1, 37) Microsoft
VBScript compilation error: Expected end of statement

errorlevel = 1

All errors are equal but some are more equal than others.

Charles

>....
>if (HaveError)
> WScript.quit(62);
>....


>// normal end of script; no error
>
>with a command file such as the following:
>
>CSCRIPT.EXE myscriptfile.js
>IF ERRORLEVEL 1 GOTO PROCESSERROR
>REM assume script must have succeeded
>
>there are potentially many cases where ERROR will be
equal to ZERO, even though the script has in fact failed.
>
>I urge Microsoft to fix CSCRIPT.EXE, so that ERRORLEVEL
of zero will only be returned when WScript.quit(n) is
called with n=0, or when the script comes to a normal
conclusion with no errors.
>
>---Wayne Erfling (Compaq Computer)
>
>

>.
>

Michael Harris (MVP)

unread,
Nov 8, 2001, 10:30:20 AM11/8/01
to
The difference is that you introduced a compilation error. The code never actually executes.

So it seems that:

- an explicit wscript.quit nnn *is* detectable as ERRORLEVEL nnn
- a compilation error is detectable as ERRORLEVEL 1
- an unhandled runtime error is *not* detectable via ERRORLEVEL

The behavior seems to be the same for both cscript and wscript as hosts.

--
Michael Harris
Microsoft.MVP.Scripting
--

"Charles Long" <charle...@eulergroup.com> wrote in message news:698901c16836$a7bc6930$a5e62ecf@tkmsftngxa07...

0 new messages