errorlevel not set in for loop - delayed expansion !errorlevel! used

G

ghquick

I do not understand why the following command file gives different
errorlevels depending on the /B switch of the exit command (it works if
not in a for loop)
Commandfiles
echo test1,test2 >token.txt
ECHO EXIT 99 >EXIT99.CMD
ECHO EXIT /B 99 >EXIT99B.CMD
echo on
setlocal ENABLEDELAYEDEXPANSION enableextensions
for /F "eol=; tokens=1,2 delims=," %%I in (token.txt) do (
CMD /V:ON /CE:EXIT99B.CMD <\nul
echo !ERRORLEVEL!
)
echo !ERRORLEVEL!
for /F "eol=; tokens=1,2 delims=," %%I in (%token.txt) do (
CMD /V:ON /CE:EXIT99.CMD <\nul
echo !ERRORLEVEL!
)
echo !ERRORLEVEL!

Gives the following output:
echo test1,test2 1>token.txt
ECHO EXIT 99 1>EXIT99.CMD
ECHO EXIT /B 99 1>EXIT99B.CMD
echo on
setlocal ENABLEDELAYEDEXPANSION enableextensions
for /F "eol=; tokens=1,2 delims=," %I in (token.txt) do (
CMD /V:ON /CE:EXIT99B.CMD 0<\nul
echo !ERRORLEVEL!
)
CMD /V:ON /CE:EXIT99B.CMD 0<\nul
echo !ERRORLEVEL!
)

c:agpw>EXIT /B 99
0

c:agpw>echo !ERRORLEVEL!
0

c:agpw>for /F "eol=; tokens=1,2 delims=," %I in (token.txt) do (
CMD /V:ON /CE:EXIT99.CMD 0<\nul
echo !ERRORLEVEL!
)
CMD /V:ON /CE:EXIT99.CMD 0<\nul
echo !ERRORLEVEL!
)
99

c:agpw>echo !ERRORLEVEL!
99
I thought both forms of the EXIT command EXITt /b 99 or EXITt 99
should return an error level of 99.
 
T

Timo Salmi

ghquick said:
setlocal ENABLEDELAYEDEXPANSION enableextensions
for /F "eol=; tokens=1,2 delims=," %%I in (token.txt) do (
CMD /V:ON /CE:EXIT99B.CMD <\nul
echo !ERRORLEVEL!
)
echo !ERRORLEVEL!

Something is missing. At the very least the latter errorlevel output
is confusing. There is no program that produces the errorlevel. An
errorlevel is to be produced by the immediately preceding program.
The general idea is this

operation1
errorlevel
operation2
errolevel

Anything else in between, or a missing operation confuseses the
errorlevel results.

Furthermore, if you need an earlier errorlevel later, the very first
thing is to store it into another, regular variable, because anything
following will destroy the errorlevel produced.

All the best, Timo
 
G

ghquick

Thanks for the reply - I was trying to create a simple case of
demonstrating errorlevel handling in FOR loops, becuase of trouble I
have been having.
I wanted to use the Exit <n> /b format of the exit command to pass up
my errors.


I now understand that my issue was with not understanding the
documentation for
Exit <n> and Exit <n> /b

In summary I have determined you want to pass error level up to the
calling command , you need to use
Exit <n> format of the command if you execcute your script with a CMD
/C command

but use the

EXIT <n> /b if you execute your script with a call command.


The following for examples show how the Exit command works in 4
different scenario


first the comand testexitb.cmd:

Rem first create two batch files to set error levels using exit
command
rem I expected both to set the errorlevel to 99 but I was wrong.
rem If the command is executed from a cmd process, leave the /b off,
becuase % errorlevel% is not passed up
rem however kind of the opposite behavior if the command is executed
by call command
ECHO EXIT 99 >EXIT99.CMD
ECHO EXIT /b 99 >EXIT99B.CMD
echo on
CMD /V:ON /CE:EXIT99B.CMD <\nul
echo exit with /b set errorlevel to %errorlevel%
rem
CMD /V:ON /CE:EXIT99.CMD <\nul
echo exit without /b set errorlevel to %ERRORLEVEL%
rem now call exit with /b
call exit99b
echo call with /b set errorlevel to %ERRORLEVEL%
now call exit without /b not script ends with no return
call exit99
echo - this line is not executed - call without /b set errorlevel to
%ERRORLEVEL%



Now the results


Rem first create two batch files to set error levels using exit command
rem I expected both to set the errorlevel to 99 but I was wrong.
rem I have to leave the /b off, becuase it only set
ECHO EXIT 99 1>EXIT99.CMD
ECHO EXIT /b 99 1>EXIT99B.CMD
CMD /V:ON /CE:EXIT99B.CMD 0<\nul
EXIT /b 99
echo exit with /b set errorlevel to 0
exit with /b set errorlevel to 0
CMD /V:ON /CE:EXIT99.CMD 0<\nul
echo exit without /b set errorlevel to 99
exit without /b set errorlevel to 99
call exit99b
EXIT /b 99
echo call with /b set errorlevel to 99
call with /b set errorlevel to 99
 
T

Timo Salmi

ghquick said:
Thanks for the reply - I was trying to create a simple case of
demonstrating errorlevel handling in FOR loops, becuase of trouble
I have been having.

You might find the following item and its examples of some interest:

38) How can I set and test the errorlevel within a script file?
167495 Jun 18 2006 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

All the best, Timo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top