By registering with us, you'll be able to discuss, share and exchange private messages with other members of our community.

SignUp Now!

Static vs dynamic for

I often use a - self-written - Perl script that renames files according to some complex condition. It is called like so:

FOR %i IN (*.pdf) rename.pl "%i"

At times it may happen that a file "abc.pdf" (e.g.) is renamed to "xyz.pdf" (e.g.). This renamed file would then be again - and unwantedly - processed by the loop as it newly appears in FOR's list of files (aka the directory).

Is there a way to have FOR use a static list of files as compared to its - obviously - dynamic one? I found no clue in the docs so far.

I know, I could change my Perl script and have IT iterate over directory...
Sorry, that's one of the oldest warnings in 4DOS and still current in the latest TCC; and it was always the way COMMAND.COM and CMD.EXE worked as well. Its reason is that it is how the file system operates.

When necessary, I circumvent it by creating my own static list. You may wish to use the feedback to request such a new feature; if you do, remember that other commands, e.g., move, are subject to the like behavior.
Another approach is to create a temp directory on the same drive, then rename the files into that location. (Yes, REN can move files as it renames.) When the loop finishes, MOVE all the renamed files from the temp directory back to their original home, then remove the temp directory.
Another option is to use DO /p:
do f in /p dir /b *.pdf (rename.pl "%f")

or using FOR:

for /f %f in ('dir /b *.pdf') do rename.pl "%f"
2) Have TCC sort the list (which creates a temp file automatically):

for /o:n in (*.pdf) rename.pl "%i"
and it has been right in the HELP manual since version 11 - I do need to RTFM!
"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."