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

Setting ERRORLEVEL in a script

6,533 views
Skip to first unread message

Norman Bullen

unread,
Jul 2, 2008, 8:22:24 PM7/2/08
to
I discovered, somewhat to my surprise, that a statement
SET VAR=
causes ERRORLEVEL to be set to one (1). However,
SET VAR=value
does not reset ERRORLEVEL, if it is non-zero, back to zero. Is there any
script command that does set ERRORLEVEL to zero?

Obviously, I can run an EXE program which is known to always return zero
but I'm hoping for a script command solution.

A answer which may apply in some cases is that you can set ERRORLEVEL
upon return from a script subroutine with the EXIT command.

CALL :subroutine
IF ERRORLEVEL 1 (
REM do something
)
GOTO :EOF

:subroutine
REM do something that sets RC to a value
EXIT /B %RC%
After the call, ERRORLEVEL will be set to the value of RC.
--
Norm

To reply, change domain to an adult feline.

Todd Vargo

unread,
Jul 2, 2008, 11:12:59 PM7/2/08
to

Try CD>NUL or VER>NUL to reset errorelvel back to zero.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

foxidrive

unread,
Jul 2, 2008, 11:11:57 PM7/2/08
to
On Wed, 02 Jul 2008 17:22:24 -0700, Norman Bullen
<no...@BlackKittenAssociates.com> wrote:

>I discovered, somewhat to my surprise, that a statement
> SET VAR=
>causes ERRORLEVEL to be set to one (1). However,
> SET VAR=value
>does not reset ERRORLEVEL, if it is non-zero, back to zero. Is there any
>script command that does set ERRORLEVEL to zero?

I was lucky, the first command I tried worked. :)

===[screen capture]===
M:\>set a=

M:\>echo %errorlevel%
1

M:\>cd .

M:\>echo %errorlevel%
0
===[screen capture]===

Timo Salmi

unread,
Jul 3, 2008, 1:17:15 AM7/3/08
to
Norman Bullen <no...@BlackKittenAssociates.com> wrote:
> Is there any script command that does set ERRORLEVEL to zero?

38} How can I set and test the errorlevel within a script file?
http://www.netikka.net/tsneti/info/tscmd038.htm

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Norman Bullen

unread,
Jul 3, 2008, 5:39:44 AM7/3/08
to
Timo Salmi wrote:

> Norman Bullen <no...@BlackKittenAssociates.com> wrote:
>
>> Is there any script command that does set ERRORLEVEL to zero?
>
>
> 38} How can I set and test the errorlevel within a script file?
> http://www.netikka.net/tsneti/info/tscmd038.htm
>
> All the best, Timo
>

Thanks to all.

Matthias Tacke

unread,
Jul 3, 2008, 9:12:42 AM7/3/08
to
Norman Bullen wrote:
> I discovered, somewhat to my surprise, that a statement
> SET VAR=
> causes ERRORLEVEL to be set to one (1).

Hi Norman,
this is only the case if VAR isn't defined.

if defined VAR set VAR=

will circumvent the errorlevel being set.

> However,
> SET VAR=value
> does not reset ERRORLEVEL, if it is non-zero, back to zero.

I guess this is by design, if simple commands like echo and set would reset
errorlevel the error handling gets more complicated.
If you want to display and save the errorlevel in a var it might be reset
otherwise.

> Is there any
> script command that does set ERRORLEVEL to zero?
>

I like foxidrives "cd ." as it does in fact nothing.

Greetings
Matthias

Herbert Kleebauer

unread,
Jul 3, 2008, 10:40:10 AM7/3/08
to
Norman Bullen wrote:

> I discovered, somewhat to my surprise, that a statement
> SET VAR=
> causes ERRORLEVEL to be set to one (1). However,
> SET VAR=value
> does not reset ERRORLEVEL, if it is non-zero, back to zero.

Rename your batch from .bat to .cmd and it will reset the errorlevel.

Timo Salmi

unread,
Jul 4, 2008, 4:46:58 PM7/4/08
to
Herbert Kleebauer <kl...@unibwm.de> wrote:
> Norman Bullen wrote:
>
>> I discovered, somewhat to my surprise, that a statement
>> SET VAR=
>> causes ERRORLEVEL to be set to one (1). However,
>> SET VAR=value
>> does not reset ERRORLEVEL, if it is non-zero, back to zero.

However, commands that _are_ supposed to return errorlevels, appear to
reset as expected. Try e.g.
@echo off & setlocal enableextensions
set var=
echo %%errorlevel%%=%errorlevel%
echo.@exit /B ^0>tmp$$$.cmd
call tmp$$$
echo %%errorlevel%%=%errorlevel%
for %%f in (tmp$$$.cmd) do if exist %%f del %%f
goto :EOF

You'll get
%errorlevel%=1
%errorlevel%=0

> Rename your batch from .bat to .cmd and it will reset the errorlevel.

While obviously true on testing, and interesting, I don't know where
this would and should take us. SET is not among the commands/programs
that is documented to return errorlevels. The same distinction does not
seem to manifest with commands that actually are supposed to return it.
If it did, the choice between .BAT and .CMD would gain a new significance.

keva...@gmail.com

unread,
May 22, 2014, 9:41:49 AM5/22/14
to
I would use the following to clear to zero:

cmd /c exit 0
echo %ERRORLEVEL%

Same method can be used to set to any integer value:

cmd /c exit 7001
echo %ERRORLEVEL%

JJ

unread,
May 23, 2014, 11:37:27 AM5/23/14
to
Faster code:

@echo off

rem generate error
cd test_not_exist>nul 2>&1
echo %errorlevel% (should be non zero)

rem clear error
cd>nul
echo %errorlevel% (should be zero)

rem set error
call :seterror 1234
echo %errorlevel% (should be as specified)

goto :eof

:seterror
exit/b %1
0 new messages