Errorlevel different outcomes.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ravishon
Posts: 1
Joined: 19 Jan 2010 05:22

Errorlevel different outcomes.

#1 Post by ravishon » 19 Jan 2010 05:30

Hi All,

I have an issue with the errorlevel setting. Here is the example scenario.

From file a.bat
call b.bat
if %errorlevel% 1 goto subBadEnd
if %errorlevel% 0 goto subGoodEnd

REM *********************************************************
REM Fault handler
REM *********************************************************
: subBADEND
echo %date% %time%: *******************************
echo %date% %time%: FAULT - err=%errorlevel%
echo %date% %time%: Dumping vars with 'set'
set
echo %errorlevel%
echo %date% %time%: *******************************
goto subEND

REM *********************************************************
REM Success handler
REM *********************************************************
: subGOODEND
echo %date% %time%: SUCCESS ok=%errorlevel%
echo %date% %time%: *******
goto subEND

REM *********************************************************
REM The end
REM *********************************************************
:subEND
echo error=%errorlevel%



File b.bat does some processing. it has the same errorchecking as above after various commands.

when b.bat fails with say errorcode 1 a.bat will finish with errorcode 0. i imagine this is because commands such as 'set' execute correctly and return an errorelvel of 0. is this correct?

when i ran this from another machine however, it ended with 1. the code may have differed slightly but im trying to understand the logic behind the errorlevels with relation to this program.

i'd very much appreciate your help.

Thanks,
R

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Errorlevel different outcomes.

#2 Post by avery_larry » 19 Jan 2010 14:37

ravishon wrote:when b.bat fails with say errorcode 1 a.bat will finish with errorcode 0. i imagine this is because commands such as 'set' execute correctly and return an errorelvel of 0. is this correct?

Basically, yes. In general (there are plenty of quirks and exceptions) the errorlevel is set after every command. So a successful echo command even should set the errorlevel back to 0 (I think).

when i ran this from another machine however, it ended with 1. the code may have differed slightly
That may be it. Hard to say without seeing all the code
but im trying to understand the logic behind the errorlevels with relation to this program.

i'd very much appreciate your help.

Thanks,
R

You can use:

exit /b 1

to exit a batch file with an errorlevel of 1 (change 1 to whatever errorlevel you want).

Post Reply