Mass rename of file extensions recursively (windows batch)

30,099

Solution 1

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.

Solution 2

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

Solution 3

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...

Share:
30,099

Related videos on Youtube

Raiden616
Author by

Raiden616

Updated on August 28, 2020

Comments

  • Raiden616
    Raiden616 over 3 years

    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!

  • Neville
    Neville over 9 years
    @Magoo... i think I love you. Thanks for this solution to a problem which took 2 days of my life, excellent work thanks!
  • Steve Chamaillard
    Steve Chamaillard over 8 years
    This worked, the selected answer didn't, said "%%i was inexpected". Thanks for this !
  • brunettdan
    brunettdan about 8 years
    Got same error "%%i was inexpected" from the code in the accepted answer. Note you'll have to cd to the start directory
  • Magoo
    Magoo about 7 years
    @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 %
  • Magoo
    Magoo about 7 years
    @brunettdan : : 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 %
  • jrhamza
    jrhamza almost 4 years
    i love this echo option. I was trying to run this, initially in a wrong directory. And echo option saved me.
  • FirstVertex
    FirstVertex over 2 years
    I wish this explained what the ~ni is for
  • Magoo
    Magoo over 2 years
    @HDog : See the documentation for /? from the prompt. ~n modifies the metavariable i to select only the Name part.