Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mass rename of file extensions recursively (windows batch)

Tags:

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!

like image 860
Raiden616 Avatar asked Jul 15 '13 15:07

Raiden616


People also ask

How do I batch rename file extensions in Windows?

How to batch rename extensions. Navigate to the folder containing the files you want. Once there, launch command prompt from the folder menu by holding down shift and right clicking on an empty space. Once in command prompt, you can now use the “ren” (for rename) command to rename for example, .


2 Answers

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 
like image 120
john smith Avatar answered Sep 23 '22 17:09

john smith


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.

like image 35
Magoo Avatar answered Sep 22 '22 17:09

Magoo