A counter in a for...loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

A counter in a for...loop

#1 Post by colargol » 14 Nov 2011 07:57

Hello

I have a text file that contains a list of files.bat
I want to execute thoses .bat and count how many returns errorlevel>0 (%nb_err%), but as I need DELAYEDEXPANSION in the for loop, I don't manage. :shock:
I tried many thing but nothing worked.
Can you help please?

Code: Select all

set nb_files=0
set nb_err=0

for /f "usebackq tokens=* delims=" %%a in (`type "%queue_list%"`) do (
   set bat_file=%%~dpna.bat
   set avs_filename=%%~nxa

   set /a nb_files+=1
   setlocal ENABLEDELAYEDEXPANSION
   echo  File !nb_files! :  !avs_filename!
   if not exist !bat_file! (
      set /a nb_err+=1
   ) else (
      call "!bat_file!"
      if "!errorlevel!" NEQ "0" (set /a nb_err+=1)
   )
   endlocal
)

echo errors: %nb_err%   &rem will display 0 of course

jeb
Expert
Posts: 1059
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: A counter in a for...loop

#2 Post by jeb » 14 Nov 2011 09:02

Hi colargol,

your problem seems to be the setlocal/endlocal block,
remove it and you could be happy.

jeb

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: A counter in a for...loop

#3 Post by colargol » 14 Nov 2011 09:31

Thanks jeb!
but if I remove setlocal, how can I get the errorlevel ???

Code: Select all

if "!errorlevel!" NEQ "0" (set /a nb_err+=1)


something like this?

Code: Select all

if "!errorlevel!" NEQ "0" (endlocal&set /a nb_err+=1) else (endlocal)
:o

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: A counter in a for...loop

#4 Post by Ed Dyreen » 14 Nov 2011 11:02

'
Agreed, place setlocal outside your for loop, otherwise you will have to push the value over manually:

Code: Select all

setlocal
set $var=hello
endlocal &set $var=%$var%
or when delayed:

Code: Select all

setlocal
set $var=hello
for %%? in ( "%$var%" ) do endlocal &set "$var=%%~?"

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: A counter in a for...loop

#5 Post by colargol » 14 Nov 2011 11:59

Thanks Ed

I didn't placed the setlocal outside the for because I think it will fail if filename have an exclamation mark.

However, I used your code and this seems to works. Great! :)

Code: Select all

for %%i in ("!errorlevel!") do (endlocal &if "%%~i" NEQ "0" (set /a "nb_err+=1"))

Post Reply