Mass rename of file extensions recursively (windows batch)
Asked Answered
C

4

24

I have numerous files in a very complex directory structure, and for reasons not worth discussing I need to rename all files with the extension of ".inp" to have ".TXT" extensions. There are numerous other files with other extensions that I do not want to be touched, and I want to do it recursively down at least 5 levels.

So far I have:

for /d %%x in (*) do pushd %%x & Ren *.inp *.TXT & popd

...but this only goes down one level of directories.

Can anyone help? Thanks in advance!

Chickenlivered answered 15/7, 2013 at 15:47 Comment(1)
Can someone supply Windows 10 solution?Bandurria
L
30
for /r startdir %%i in (*.inp) do ECHO ren "%%i" "%%~ni.txt"

should work for you. Replace startdir with your starting directoryname and when you've checked this works to your satisfaction, remove the echo before the ren to actually do the rename.


For the downvoters: executing a batch file differs from excuting from the command prompt in that each %%x where x is the metavariable (loop-control variable) needs to be reduced to %, so

for /r startdir %i in (*.inp) do ECHO ren "%i" "%~ni.txt"

should work if you execute this from the prompt. Please read the note about echo.

Lotus answered 15/7, 2013 at 16:7 Comment(4)
@Magoo... i think I love you. Thanks for this solution to a problem which took 2 days of my life, excellent work thanks!Pucka
i love this echo option. I was trying to run this, initially in a wrong directory. And echo option saved me.Kaitlin
I wish this explained what the ~ni is forBeltz
@HDog : See the documentation for /? from the prompt. ~n modifies the metavariable i to select only the Name part.Lotus
S
46

On Windows 7, the following one-line command works for me, to rename all files, recursively, in *.js to *.txt:

FOR /R %x IN (*.js) DO ren "%x" *.txt
Szombathely answered 18/10, 2014 at 10:45 Comment(4)
This worked, the selected answer didn't, said "%%i was inexpected". Thanks for this !Cortez
Got same error "%%i was inexpected" from the code in the accepted answer. Note you'll have to cd to the start directoryJohnson
@SteveChamaillard : That's because the selected answer assumes the line is in a batch file. If it's executed from the prompt, you need to change each %% to %Lotus
@Johnson : : That's because the selected answer assumes the line is in a batch file. If it's executed from the prompt, you need to change each %% to %Lotus
L
30
for /r startdir %%i in (*.inp) do ECHO ren "%%i" "%%~ni.txt"

should work for you. Replace startdir with your starting directoryname and when you've checked this works to your satisfaction, remove the echo before the ren to actually do the rename.


For the downvoters: executing a batch file differs from excuting from the command prompt in that each %%x where x is the metavariable (loop-control variable) needs to be reduced to %, so

for /r startdir %i in (*.inp) do ECHO ren "%i" "%~ni.txt"

should work if you execute this from the prompt. Please read the note about echo.

Lotus answered 15/7, 2013 at 16:7 Comment(4)
@Magoo... i think I love you. Thanks for this solution to a problem which took 2 days of my life, excellent work thanks!Pucka
i love this echo option. I was trying to run this, initially in a wrong directory. And echo option saved me.Kaitlin
I wish this explained what the ~ni is forBeltz
@HDog : See the documentation for /? from the prompt. ~n modifies the metavariable i to select only the Name part.Lotus
O
7

John Smith's answer is excellent, and it works. But to be completely clear (I had to re-read magoo's notes to figure out the correct syntax), here is exactly what you need to do...

BATCH FILE:
FOR /R %%x IN (*.js) DO ren "%%x" *.txt

COMMAND LINE:
FOR /R %x IN (*.js) DO ren "%x" *.txt

Up vote their responses, I am but a lowly formater...

Owenowena answered 28/8, 2018 at 1:36 Comment(0)
A
0

Sometimes the renaming is necessary to change invalid characters inside the files and names. If you need to change a single character recursively:

Change character "╞" for "ã" in folder names recursively:

FOR /R /D %x in (*╞*) do @for /f "tokens=1-3* delims=╞" %B in ("%x") DO ren "%x" "%~nBã%C"

Change character "╞" for "ã" in file names recursively:

FOR /R %x in (*╞*.*) do @for /f "tokens=1-3* delims=╞" %B in ("%x") DO ren "%x" "%~nBã%C"

Change the chars for whatever you need. If you want to test your renaming commands before executing, just put an @echo between DO and ren:

DO @echo ren "%x" "%~nBã%C"

This will output the commands instead of executing them

Alagez answered 17/5, 2022 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.