Link to home
Start Free TrialLog in
Avatar of LostONE
LostONE

asked on

Loop Counting in a Batch File

I have this little loop to copy and delete files to a zip disk.  I am using this to test the longevity of the disk.  I would like to add a counting routine to this loop to display how many times the loop has ran.  Can someone please help me on this.

:Loop
c:
echo
echo Copying Files
echo
copy *.* h:
h:
echo
echo Deleting Files
echo
del /q *.*
goto Loop
ASKER CERTIFIED SOLUTION
Avatar of pbarrette
pbarrette

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This loop counter should work in all version of DOS.  One caviate is you need the find.exe to be present.

Usage of program

Batfilename loopcount

in otherwords batfilename 999 will make the program exit after 999 loops


SET T1=%temp%\TEMP.BAT
SET T2=%temp%\Set.BAT
SET T3=%temp%\Count.BAT
Echo.! > %T3%

:Loop

<insert your code here>

echo e100'SET %%1='> %T1%
for %%? in (rcx 7 n%T2% w q) do echo %%?>> %T1%
DEBUG < %T1% > nul
type nul > %T1%
find /c "!" < %T3% >> %T2%
%T2% num
Echo Loop %num%
If %1 = %num% Then goto Exit
Goto Loop

:Exit
Oh and if you dont put any numbers after batfilename or whatever you wish to call this.  It will loop forever (or at least until you CTRL C the bat file.

Prob should put this after End for cleanup purposes

del %T1%
del %T2%
del %T3%
OK my bad looks like I forgot to put a call function in one spot.  This one works I actually tested it :)

@ Echo Off
SET T1=%temp%\TEMP.BAT
SET T2=%temp%\Set.BAT
SET T3=%temp%\Count.BAT
Echo.! > %T3%

:Loop

rem <insert your code here>

echo e100'SET %%1='> %T1%
for %%? in (rcx 7 n%T2% w q) do echo %%?>> %T1%
DEBUG < %T1% > nul
type nul > %T1%
find /c "!" < %T3% >> %T2%
Call %T2% num
Echo Loop %num%
Echo.! >> %T3%
If "%1" == "%num%" goto Exit
Goto Loop

:Exit
del %T1%
del %T2%
del %T3%
My utility Count will do this (in addition to integer and floating point arithmetic).  Try it from my site below - it's free.

--
Paul Doherty
http://ped.deadartists.com
DOS/Windows Utilities
Avatar of mC
mC

I'm assuming its not under NT/2k/xp as if it were you could simply add:

:start
set counter=0

:loop
set /a counter=%counter%+1
<code>
goto loop
mC,

I think I posted that already. The counter code should probably go to the end though. This will ensure that a count is only added if the loop was successful.

pb
Oops sorry. Was wandering the net at 5am for an update to a tool I use input.exe by M.Wilson (never found it if you know of one). Started blindly answering questions so didnt see that there, havent done the old forums thing for quite a while now, bit of fun I must say. Used to do the freesco one (Linux router/firewall).

MC
Hi LostONE
This question is still open and needs to be closed. If any of the comments above helped you, please accept it as an answer. If not please send an update about your issue so that the question can be finalised. Thank you

*** PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER ***

Pasha

Cleanup Volunteer
LostONE,
No comment has been added lately (42 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to pbarrette http:#7748622

Please leave any comments here within 7 days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

cempasha
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post a request in Community support (with a link to this page) to refund your points. https://www.experts-exchange.com/Community_Support/

Per recommendation.

YensidMod
EE Moderator