REN and FOR %a with files

Jun 7, 2008
121
0
Hi

Running TCC 13.02.35 Windows XP [Version 5.1.2600]

1. Create 3 files with names

20111129.PR2
20111130.PR2
20111128.PR2

in an empty directory. They are non-zero in size.

2. Now do
for %a in (*.pr2) do ren %a B%a

3. I find that the for loop runs 4 times. It does the renaming for the 3 files and then does the 1st file again. I see

B20111129.PR2
B20111130.PR2
BB20111128.PR2

Anyone reproduce this ?

thanks

Stephen Howe
 
Jan 19, 2011
604
14
Norman, OK
Anyone reproduce this ?
Interesting. I tried it first just using zero size files 1.txt, 2.txt, and 3.txt and it worked fine, renaming just the three files. When I used your example verbatim, I got the same problem you got.

HTML:
[C:\BIN\temp]
09:22:52 $ > 1.txt
 
[C:\BIN\temp]
09:22:57 $ > 2.txt
 
[C:\BIN\temp]
09:23:01 $ > 3.txt
 
[C:\BIN\temp]
09:23:04 $ dir
 
Volume in drive C is unlabeled      Serial number is 58b8:a853
Directory of  C:\BIN\temp\*
 
1/06/2012  09:23        <DIR>    .
1/06/2012  09:23        <DIR>    ..
1/06/2012  09:22              0  1.txt
1/06/2012  09:23              0  2.txt
1/06/2012  09:23              0  3.txt
                0 bytes in 3 files and 2 dirs
    33,034,014,720 bytes free
 
[C:\BIN\temp]
09:23:05 $ for %i in (*.txt) do ren %i 1%i
C:\BIN\temp\1.txt -> C:\BIN\temp\11.txt
    1 file renamed
C:\BIN\temp\2.txt -> C:\BIN\temp\12.txt
    1 file renamed
C:\BIN\temp\3.txt -> C:\BIN\temp\13.txt
    1 file renamed
 
[C:\BIN\temp]
09:23:31 $ > 20111129.PR2
 
[C:\BIN\temp]
09:23:53 $ > 20111130.PR2
 
[C:\BIN\temp]
09:24:00 $ > 20111128.PR2
 
[C:\BIN\temp]
09:24:05 $ for %a in (*.pr2) do ren %a B%a
C:\BIN\temp\20111128.PR2 -> C:\BIN\temp\B20111128.PR2
    1 file renamed
C:\BIN\temp\20111129.PR2 -> C:\BIN\temp\B20111129.PR2
    1 file renamed
C:\BIN\temp\20111130.PR2 -> C:\BIN\temp\B20111130.PR2
    1 file renamed
C:\BIN\temp\B20111128.PR2 -> C:\BIN\temp\BB20111128.PR2
    1 file renamed
 
[C:\BIN\temp]
09:24:29 $ ver
 
TCC  13.03.36  Windows XP [Version 5.1.2600]
 
Jun 28, 2008
69
2
Chicago
www.pement.org
I know that TCC LE is not supported here, but here's a screen dump:

Code:
[c:\tmpx] > ver
 
TCC LE  13.03.36  Windows 7 [Version 6.1.7601]
 
[c:\tmpx] > for %f in ( 28.pr2 30.pr2 29.pr2 ) do echo hello >%f
 
[c:\tmpx] > dir /hklm *.pr2
2012-01-06  9:32              7  28.pr2
2012-01-06  9:32              7  29.pr2
2012-01-06  9:32              7  30.pr2
 
[c:\tmpx] > for %a in ( *.pr2 ) do ren %a B%a
C:\tmpx\28.pr2 -> C:\tmpx\B28.pr2
    1 file renamed
C:\tmpx\29.pr2 -> C:\tmpx\B29.pr2
    1 file renamed
C:\tmpx\30.pr2 -> C:\tmpx\B30.pr2
    1 file renamed
C:\tmpx\B28.pr2 -> C:\tmpx\BB28.pr2
    1 file renamed
 
[c:\tmpx] > dir /hklm *.pr2
2012-01-06  9:32              7  b29.pr2
2012-01-06  9:32              7  b30.pr2
2012-01-06  9:32              7  bb28.pr2
 
May 20, 2008
3,515
4
Elkridge, MD, USA
The phenomenon is an artifact of wildcard expansion. It has been a CAVEAT for renamin, moving, or copying by any program which obtains the names of files to be processed from the file system one-at-a-time that if the result of command action matches the input criteria, it may be acted on more than once.
AFAIK all programs for the PC environment behave this way. Cf: Most POSIX-compatible shells expand wildcards before processing the first match, so they do not have this issue.
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
In the full TCC, you can work around this issue by using REN /O:A to generate the list of files to be renamed before beginning the actual operation. I don't think that option is available in TCC/LE, though.
 
Jun 28, 2008
69
2
Chicago
www.pement.org
Stephen, if you do not have full TCC, as a workaround you can use renx.btm, a batch file I wrote several years ago, which calls Perl to do its file renaming.

Since it's a batch file, I will post the file in the "Tips and Tricks > Scripting" forum, not here . . .

Eric
 
Jul 1, 2008
81
0
74
Montreal
In the full TCC, you can work around this issue by using REN /O:A to generate the list of files to be renamed before beginning the actual operation. I don't think that option is available in TCC/LE, though.
Good idea. For a long time I have tailored such constructs to avoid the problem. For example I might have written Stephen's code as
Code:
for %a in (2*.pr2) do ren %a B%a
but this is more challenging when filenames are not similar. Your suggestion offers an easier approach.

Peter
 
Jun 7, 2008
121
0
Good idea. For a long time I have tailored such constructs to avoid the problem. For example I might have written Stephen's code as
Code:
for %a in (2*.pr2) do ren %a B%a
but this is more challenging when filenames are not similar. Your suggestion offers an easier approach.
Peter

That is exactly what I wound up doing. No ambiguity.
I have the full version of TCC and Take Command, not TCC/LE. Been using JPSoft's command processors since 4DOS 3.0.

Thanks all

Stephen Howe
 
May 20, 2008
3,515
4
Elkridge, MD, USA
In the full TCC, you can work around this issue by using REN /O:A to generate the list of files to be renamed before beginning the actual operation. I don't think that option is available in TCC/LE, though.
Would /o:u be faster, esp. when the file system does not keep the directory sorted? Of course, the difference would be in the sub-second range, insignificant under virtually all conditions...
 
Similar threads
Thread starter Title Forum Replies Date
Jesse Heines Using Regular Expressions with the REN commanc Support 8
C Documentation Help Nit. REN / RENAME Support 0
D How to? Use regular expression with REN? Support 2
vefatica REN and NTFS streams? Support 3
samintz REN implied .* Support 2
F Regexes in REN/MOVE/… commands seem to not work at all Support 3
D How to? Wildcards as sets - looping issue with FOR and REN Support 7
samintz WAD Weird REN bug Support 6
nickles REN bug Support 11
Phileosophos ren /s does not process subdirectories Support 3
M Ren command confusion Support 13
S Strange REN problem - non-English characters Support 3
T REN a TCC Tab from command line Support 9
MickeyF TCC crashing when copying multiple files (now resolved) Support 6
fpefpe How to? shortcuts and batch files Support 4
C v28 key files Support 2
J ffind does not find files Support 4
C duplicate files in multiple plugin sites Support 9
Dmitry L. Kobyakov Fixed The SETARRAY /R command works incorrectly with cyrillic files Support 2
S How to? Deinstalling TC/reinstating execution of bat files by cmd in Win7 Support 31
vefatica FFIND, temp files? ... not deleted? Support 0
R FUNCTION to count NUMBER of files matching a pattern and specific length/not length Support 4
vefatica Tmp files from in-process pipes not deleted Support 6
vefatica Finding files with alternate data streams? Support 4
rps Multi-column DIR /v not displaying all files. Support 5
D How to skip files in a fileset? Support 6
vefatica PIPEVIEW leaves TMP files Support 18
K "copy /z /w" command not deleting read-only files in destination Support 10
Jesse Heines "Taken" date for JPG and PNG files Support 4
O I cannot use links to .btm files Support 5
C How to? rename files with spaces Support 8
R How to? Append files in multiple subfolders using copy? Support 8
Joe Caverly "Functions" in cmd.exe batch files Support 6
S Batch files Calling Batch Files Support 2
vefatica Find Files/Text dialog: does it work? Support 0
Joe Caverly UNZIPping older .ZIP files Support 3
C Documentation default location for LOG files Support 21
P exit /b in batch files Support 7
P Labels in batch files: TCC vs CMD.exe Support 9
M ANSI is used to show files instead of OEM in version 17 to 19 ! Support 5
C 7Z files on forum? Support 0
x13 Problem listing repository files using DIR http(s)://... Support 8
B Can copy command concatenate multiple mp3 files Support 2
redwdc Copying files with spaces, commas, etc. Support 5
J New lexer for TCC batch files. Support 0
M How to? want to download a file from a website enumarate the links and download the files Support 2
keithg1964 TCC RT Install Files Support 1
P Fixed Rexx .cmd files no longer work in TCC 20 Support 3
thorntonpg Handling files with embedded spaces Support 1
WavSlave Fixed Unable to sort files by description in TCC v19 Support 2

Similar threads