Echo subtotal counter within a for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Gelon
Posts: 2
Joined: 20 Feb 2012 09:30

Echo subtotal counter within a for loop

#1 Post by Gelon » 20 Feb 2012 09:36

Dear Support Forum

I'm a bit new to the whole bat files, but Google is my friend ;)
Nonetheless I'm encountering a problem I can not find on Google, so hopefully you can help me out.

In the script below, I have a counter running to count the amount of missing files in a folder. The counter it self works, because the total is correct. However when I display the number it always shows 0 (with echo).
Does someone know why this happens and more preferably how I can avoid it and display the right number ;)

Code: Select all

@echo off

::DATE+TIME
set mydate=%date:~-4,4%%date:~-7,2%%date:~-10,2%
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)

::LOG FILE NAME
SET LOGFILE=E:\compare_log_%mydate%_%mytime%.txt

echo ---------------------------------------- >> %LOGFILE%
echo Compare LocationCode (E) with ScanIn (C) >> %LOGFILE%
echo Date: %mydate% %mytime%                  >> %LOGFILE%
echo ---------------------------------------- >> %LOGFILE%

SET DEST=C:\DEST
SET SRC=E:\SOURCE
SET /A COUNT=0
SET /A LOCCOUNT=0

:: Notify user of action
echo ----------------------------------------
echo Compare SOURCE (E) with DEST (C)
echo Date: %mydate% %mytime%                 
echo ----------------------------------------
echo Scanning all folders and files in
echo %SRC%
echo for the existence of the log file in
echo %DEST%
echo This proces takes a few seconds

cd %SRC%

for /f "usebackq delims=|" %%f in (`dir /b %SRC%`) do (   
   cd %SRC%\%%f

   SET /A LOCCOUNT=0

   echo LocationCode: %%f >> %LOGFILE%
   for %%x in (*.log) do (
      if NOT exist %DEST%\%%x (
         echo Missing: %%x             >> %LOGFILE%
         set /a LOCCOUNT+=1
      )
   )
   set /a COUNT+=LOCCOUNT
   echo %%f : %LOCCOUNT%
   echo ---------------------------------------- >> %LOGFILE%
)

cd ..

echo --------------##FINISED##--------------- >> %LOGFILE%
echo Total: %COUNT%                           >> %LOGFILE%
echo ---------------------------------------- >> %LOGFILE%

echo ----------------------------------------
echo ----------##Process finished##----------
echo Total: %COUNT%
echo Logfile: %LOGFILE%
echo ----------------------------------------
PAUSE

LocationCode is a result from the dirname

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Echo subtotal counter within a for loop

#2 Post by Squashman » 20 Feb 2012 12:38

You need to enable delayed expansion and then use exclamations around your variable name instead of the percent signs.

Gelon
Posts: 2
Joined: 20 Feb 2012 09:30

Re: Echo subtotal counter within a for loop

#3 Post by Gelon » 20 Feb 2012 14:45

Thanks for the quick reply will try that tomorrow ;)

Will use http://ss64.com/nt/delayedexpansion.html for more info (and for other who find this topic ;))

Post Reply