for ... do loop "wrap around"

Dec 26, 2009
29
2
No where
for %f in (*.mkv) do (
echo %f >> "E:\Documents\Computer\Files Uploaded.txt"
)

This results in E:\Documents\Computer\Files Uploaded.txt having the following contents:

file1.mkv
file2.mkv
file3.mkv
file4.mkv
file1.mkv

Note that file1.mkv is there twice. After processing everything, the for...do loop "wraps around" back to the beginning. This is a minor annoyance and not a big deal.

However, in other batch files I use, I am doing a lot of other processing and renaming files in a very specific way. When the loop "wraps around" one or more files get processed again and it really screws things up.

As a work-around I include extra code that moves each file out of the directory after it is processed and then moves it back when finished. But that's kind of a pain and shouldn't be necessary.

This happens with TCC versions 24,25 and 26.
 
Last edited:
Jun 2, 2008
376
2
Newton, MA
I'm still using version 25, and I do not see that behavior. I ran the following command:

for %f in (*.htm) do (echo %f >> junk.txt )

There were 49 matching files in the directory, and there were 49 line in junk.txt. I thought that perhaps the issue arose only when the command was broken over several lines, so I tried the same three-line version as in your example, and I again got the correct result. The variable %_for_files also had the correct value.

-- Jay
 
May 20, 2008
11,407
99
Syracuse, NY, USA
I don't see it either (v26).

Code:
v:\test> dir /k /m /h
2020-07-29  17:01               0  file1.mkv
2020-07-29  17:01               0  file2.mkv
2020-07-29  17:01               0  file3.mkv
2020-07-29  17:01               0  file4.mkv

v:\test> for %f in (*.mkv) do (
More? echo %f >> "log.txt"
More? )

v:\test> type log.txt
file1.mkv
file2.mkv
file3.mkv
file4.mkv
 
Jun 2, 2008
376
2
Newton, MA
Charles G.: I was just duplicating Dick Johnson's command to see if I experienced the problem he reported (I did not). Your command probably would have worked for him, since it did not have the compound command.

Dick: Do you see the problem with the simpler command

for %f in (*.mkv) do echo %f
 

rconn

Administrator
Staff member
May 14, 2008
12,350
150
Not reproducible here.

However, this sort of thing is not uncommon when processing files if you're also modifying / creating the file list at the same time. DO is doing a findfirstfile/findnextfile to get the file list, and if something else is simultaneously writing the directory, Windows can return the same file multiple times. You can get around this by using the /O: option in FOR, which will retrieve all of the files before doing any processing on the list.
 
Dec 26, 2009
29
2
No where
Charles G.: I was just duplicating Dick Johnson's command to see if I experienced the problem he reported (I did not). Your command probably would have worked for him, since it did not have the compound command.

Dick: Do you see the problem with the simpler command

for %f in (*.mkv) do echo %f
No, I tried your "simpler version" and it worked fine. But that's the thing. The "wrap-around" problem does not occur 100% of the time. It happens frequently, but not 100%.

It just happened to me again today (using TCC 27.00.18 x64).

I have a batch file that I use to rename large groups of files in a very specific way. When I ran it today, it renamed everything perfectly, but then "wrapped around" and processed the first file again, completely screwing up the file name.

I will try the /O option with For, as recommended by rconn.

edit: Well, what do you know. It's right there in the documentation.

"The /O:... option saves all of the matching filenames and then performs the requested operation. This avoids the potential problem of processing files more than once."

Of course, you have to know where to look, and what you're looking for. :)
 
Last edited:

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,464
88
Albuquerque, NM
prospero.unm.edu
This kind of issue is inherent to FindFirstFile / FindNextFile, not just to TCC. If you make changes to the directory in question between FindNextFile calls, those changes may or may not be reflected in later FindNextFile calls, depending on a number of factors including the exact contents of the directory and even the file system of the drive being modified.
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
Another option is to execute the DIR command and capture its output and work from that list.
Code:
do f in /p dir /b *.mkv (echo %f)
for /F %%f in ('dir /b') do echo %%f
 
Similar threads
Thread starter Title Forum Replies Date
samintz How to? Abort on error DO loop Support 10
D How to use multi-line DO to loop through first level directory names Support 5
vefatica ON ERROR terminates DO loop. Support 0
vefatica Modifying a counted DO loop's counter? Support 1
Z [dos] into - for loop - the bp is not hit Support 5
vefatica Piping the output of a DO loop. Support 4
rps For loop internal %_for_files variable? Support 2
D What is the limit on the compound statement length within ( ) of a FOR-loop? Support 5
old coot Proper use of ON ERRORMSG inside a DO WHILE loop? Support 3
H EndLocal in DO Loop Support 2
B How to? redirect a bunch of loop Support 4
Mike Hall Unexpected "Variable loop" in IF condition Support 10
C FOR loop with nested IF / IFF Support 9
D Strange issue with FOR loop Support 15
nickles WAD Variable name gets changed within FOR loop Support 2
M tcmd crashes in this recursive "for /f" loop Support 4
G Fixed Slow FOR loop Support 2
S WAD %_do_errors does not report errors when loop control variable type is not directory entry Support 1
mikea 'Loop' on closing TC Support 6
Roedy Loop anomaly Support 6
R nested for loop question Support 3
P Need to use a regex in a "for" loop. Support 54
vefatica VIEW wrap searches? Support 2
C View Print Wrap - please break at whitespace Support 3
C VIEW PRINT /Wrap doesn't work Support 0
R A problem using find in list when word wrap? Support 2
Jay Sage Brackets Around Variable Names Support 11
M Is there any way to work around this (apparent) limitation of TCC? Support 59
M How to? Work around an unexpected and very inconvenient behavior of TCC... Support 26
M What can I do to "word around" this problem? Support 8
M Is there a way to get around this limitation of file names? Support 12

Similar threads