Re: errorlevel handling in DOS batch



On Tue, 10 Jun 2008 06:07:36 -0700, bubu.aa wrote:

http:/hi
I have a question
in a toplevel batch I have some thing like:

call mybat
if NOT (%ERRORLEVEL%) == (0) echo error

That will always fail fail because you are testing (assuming ERRORLEVEL is
0)
if not "(0) "==" (0)"

Note the locations of the spaces prevents a valid test - the correct
syntax is

if not (%errorlevel%)==(0)
but I would advise against using () there since they have syntatical
functions - it is usual to use "" or to add a punctuation character (!,
for example) to avoid the possibility of testing a null string.


in mybat I have some thing like
///////////////////////////////////////////////////////////////
pushd %~dp0
........
call mybat2
GOTO END
:END
echo,
popd
///////////////////////////////////////////////////////////////

mybat2 returns an errorlevel.

We have ony your word for that - you omitted the code that would allow us
to decide whether or not you actually are diong that - that syntax
problems with the test indicates that suspicion on the exit code point is
warranted.

Is it guaranteed that I will see the errorlevel returned by mybat2 in
the top level batch file?

Or in other words will these DOS commands change the errorlevel (for
example on different OS???)?
GOTO END
:END
echo,
popd


Some commands that were internal commands in COMMAND.COM are externals
under CMD.EXE and may return their own errorlevels. ERRORLEVEL is
*always* tested immediately after the function that sets it returns just
as the return value of a function in high level languages is always tested
immediately after return (or is stored in a stable variable or use later).

Unless that rule is followed, there is the risk of inserting code later
between the function and the test - code that breaks the program. If it
is necessary to test a return code later rather than immediately, save it
in a dedicated variable.


--
T.E.D. (tdavis@xxxxxxx)




.