Re: Batch Counter in For-Do loop

From: Al Dunbar [MS-MVP] (alan-no-drub-spam_at_hotmail.com)
Date: 12/30/04

  • Next message: nsyforce_at_aol.com: "Fragment Identifiers and posting to server"
    Date: Thu, 30 Dec 2004 11:28:34 -0700
    
    

    "homerlex" <deja@homerlex.mailshell.com> wrote in message
    news:1104430541.929348.148090@z14g2000cwz.googlegroups.com...
    > 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


  • Next message: nsyforce_at_aol.com: "Fragment Identifiers and posting to server"

    Relevant Pages

    • Re: log off command
      ... I simply execute the batch file and let them play... ... If it's by the hour you don't need a script. ... You can logoff a sessionname or a session ID in each case you have to ... Is it possible to issue the command from user1's logon to logoff ...
      (microsoft.public.windowsxp.basics)
    • Re: log off command
      ... I simply execute the batch file and let them play... ... If it's by the hour you don't need a script. ... You can logoff a sessionname or a session ID in each case you have to ... Is it possible to issue the command from user1's logon to logoff ...
      (microsoft.public.windowsxp.basics)
    • Re: Command or setting to force monitor on?
      ... Keystrokes vbs script? ... But the same symptoms as without cscript, monitor not waking up, when I ... I set up a batch file containing the commands to first start IE, ... could move to the keystrokes command. ...
      (microsoft.public.windowsxp.general)
    • Re: Scheduled script problem
      ... A vbs script that sets up a command to run a batch file, calls EXEC on the ... until the scheduler stops the script after the maximum allowed time to run. ...
      (microsoft.public.windows.server.scripting)
    • Re: Scheduled script problem
      ... A vbs script that sets up a command to run a batch file, calls EXEC on the ... until the scheduler stops the script after the maximum allowed time to run. ...
      (microsoft.public.windows.server.scripting)