for ... do loop "wrap around"

Dec 26, 2009
22
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
312
1
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
10,574
78
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
312
1
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
11,915
133
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.