PC Review


Reply
Thread Tools Rate Thread

Batch Counter in For-Do loop

 
 
homerlex
Guest
Posts: n/a
 
      30th Dec 2004
I have a batch file that executes a bunch of apps and counts the
errors. The issue is that I can't see the current error count from
within the for loop.

I've stripped down the batch file to the following. When I run it the
ERROR_COUNT is shown properly at the end but always shows as zero in
the for loop. What am I doing wrong?

I have similar issue testing %errorlevel% from within the loop (though
this is not shown in the sample below)

@Echo Off

SET EXECUTION_LIST=1.exe 2.exe 3.exe 4.exe
SET ERROR_COUNT=0


FOR %%d in (%EXECUTION_LIST%) do (
echo ****************************************
echo Running: %%d
echo ----------------------------------------
rem %%d
SET /A ERROR_COUNT+=1
echo %ERROR_COUNT%
echo ----------------------------------------
echo ****************************************
)

echo Errors: %ERROR_COUNT%

 
Reply With Quote
 
 
 
 
Al Dunbar [MS-MVP]
Guest
Posts: n/a
 
      30th Dec 2004

"homerlex" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a batch file that executes a bunch of apps and counts the
> errors. The issue is that I can't see the current error count from
> within the for loop.
>
> I've stripped down the batch file to the following. When I run it the
> ERROR_COUNT is shown properly at the end but always shows as zero in
> the for loop. What am I doing wrong?


The bracketted set of commands immediately following the "do" command are
processed as one command. The main effect is that all %variable% references
are resolved ONCE ONLY just before the first command in the list is
executed. Even though the variable's value changes through the execution in
the loop, the "echo %error_count%" still shows its initial value for the
reason I have just recounted.

There are two solutions, the first one being to enable delayed variable
expansion by putting this command at the front of your batch script:

setlocal enabledelayedexpansion

and then change the echo command to:

echo !error_count!

Note that the setlocal command will have side effects that might cause
unexpected problems depending on what else your script does, the main one
being that any changes made by the script to variables (i.e. to pass
information back to the calling script) will be rolled back.

The second solution is to move the problematic reference to an internal
subroutine as illustrated below:

> I have similar issue testing %errorlevel% from within the loop (though
> this is not shown in the sample below)


IMHO, this is the same problem...

> @Echo Off
>
> SET EXECUTION_LIST=1.exe 2.exe 3.exe 4.exe
> SET ERROR_COUNT=0
>
>
> FOR %%d in (%EXECUTION_LIST%) do (
> echo ****************************************
> echo Running: %%d
> echo ----------------------------------------
> rem %%d
> SET /A ERROR_COUNT+=1


replace the following line:

> echo %ERROR_COUNT%


with this:

call:show_error

> echo ----------------------------------------
> echo ****************************************
> )
>
> echo Errors: %ERROR_COUNT%


and add the show_error routine:

goto:eof
:show_error
echo %error_count%
goto:eof


/Al


 
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
Do While Loop using time as the counter =?Utf-8?B?YXNtZW51dA==?= Microsoft Excel Programming 2 26th Sep 2006 11:58 PM
Adding a counter to a loop =?Utf-8?B?R3JhbnQ=?= Microsoft Access Form Coding 3 8th Jul 2006 12:30 AM
Should I use Do-While loop for my record counter? excelnut1954 Microsoft Excel Programming 0 24th Mar 2006 09:25 PM
Counter within FOR Loop David Ashe Microsoft Windows 2000 CMD Promt 2 12th Nov 2004 11:02 PM
On Screen Loop Counter simoncohen Microsoft Excel Programming 3 2nd Apr 2004 10:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:28 AM.