I\'m facing a weird situation where a batch file I wrote reports an incorrect exit status. Here is a minimal sample that reproduces the problem:
bug.cmd
@dbenham's answers are good. I am not trying to suggest otherwise. But, I have found it reliable to use a variable for the return code and a common exit point. Yes, it takes a few extra lines, but also allows additional cleanup that, if necessary, would have to be added to every exit point.
@ECHO OFF
SET EXITCODE=0
if "" == "" (
echo first if
set EXITCODE=%ERRORLEVEL%
GOTO TheEnd
if "" == "" (
echo second if
)
)
:TheEnd
EXIT /B %EXITCODE%