Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am attempting to 'rename' 3 .csv files in a directory using the following 'for' command in a batch file:
for %%i in (*.csv) do (rename "%%i" "NewName%%i")
The 3 files get renamed & then the first file in the directory gets renamed again to NewNameNewName.csv....
Why is this happening?

Because the resulting file is conform to the same initial requirement (*.vbs) ... shortcut that some way or another, and he will do it correct

Thank you for your reply. I'm not sure I understand. The 3 original files are:
file1.csv
file2.csv
file3.csvThe result is:
NewNameNewNamefile1.csv
NewNamefile2.csv
NewNamefile3.csv

This comes up from time to time.
There appears to be a race condition when you combine FOR and REN. The workaround is to use FOR /F:for /f "delims=" %%a in ('dir /b *.csv') do ren "%%a" "NewName%%a"

If it was DOS, which it isn't:
ren *.csv N*.#
ren *.# *.csv
=====================================
Helping others achieve escape felicityM2

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |