Windows – How to Loop Through Folders and Rename Extensions in a Batch File

batch filebatch-renamerenamewindows

Selecting a single folder I could run

ren *.jpeg *.png

I have multiple folders. I want to create a batch file that will loop through all the folders and will rename the files within it.

For example I have folders TEST1 and TEST2 inside root directory TEST. I want to create a batch that will rename all the .jpeg files within TEST1 and TEST2 to .png.

FOR /R "E:\TEST\" %%G in (*.jpeg) DO ren *.jpeg *.png

I am getting an error:

The system can not find the file specified

I don't understand the issue.

Best Answer

You are not applying the for command to the ren action.

for /r "E:\test\" %%G in (*.jpeg) do ren "%%~G" *.png

You need to change %% to % if you are doing this interactively, and not in a batch file.

The ~ strips quotes, which are re-added, to avoid any possible errors with paths which contain spaces.

Related Question