PC Review


Reply
Thread Tools Rate Thread

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

 
 
ghquick
Guest
Posts: n/a
 
      5th Jul 2006
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!
)

>EXIT 99

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.

 
Reply With Quote
 
 
 
 
Timo Salmi
Guest
Posts: n/a
 
      6th Jul 2006
ghquick wrote:
> 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

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
private.php?do=newpm&u= <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
 
Reply With Quote
 
 
 
 
ghquick
Guest
Posts: n/a
 
      6th Jul 2006
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


>echo on


>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

>rem


>CMD /V:ON /CE:EXIT99.CMD 0<\nul


>EXIT 99


>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

>call exit99


>EXIT 99


 
Reply With Quote
 
Timo Salmi
Guest
Posts: n/a
 
      7th Jul 2006
ghquick wrote:
> 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

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
private.php?do=newpm&u= <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
For Loop and ErrorLevel Praetorian Guard Microsoft Windows 2000 CMD Promt 19 12th Mar 2009 03:13 PM
How to set errorlevel env variable properly from a simple batch file: Answered rajenk@gmail.com Windows Vista General Discussion 1 7th Nov 2007 01:34 AM
robocopy Version XP026 errorlevel not set alex.taylor@comcare.gov.au Windows XP General 1 6th Sep 2007 08:50 AM
how do i set errorlevel when leaving a batch file? Anthony Ewell Windows XP General 11 24th Jun 2005 10:29 PM
Infinite Loop Infinite Loop Infinite Loop Infinite Loop... Donna Windows XP Setup 0 8th Dec 2003 11:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:11 PM.