Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Batch change file extensions within subfolders [duplicate]

I am very new to Command Prompt, and only started using it as of 1 day ago.

I have a folder in a location, for example C:\Users\Administrator\Desktop\Images, and inside that folder there is roughly 650 sub-folders, each containing around 20 images, a mix of JPG's and PNG's. I am looking for a command line for CMD which will go through all sub folders and change each .png file into a .jpg file.

I have done a little research and found some information, however it is very hard to follow and understand, and I am still unable to do it. I am wanting to keep the file names, however change each file extension from a .png to a .jpg.

I understand that for 1 folder, the line is something like ren *.png *.jpg. However, this does not apply changes to subfolders.

like image 956
Jai Burrell Avatar asked Mar 10 '13 00:03

Jai Burrell


People also ask

How do I change the extension of multiple files in PowerShell?

Change file extensions with PowerShell You can also use the Rename-Item to change file extensions. If you want to change the extensions of multiple files at once, use the Rename-Item cmdlet with the Get-ChildItem cmdlet.


1 Answers

If I understand correctly that you only want to rename the files from .png to .jpg, and not convert them, you could use the following batch code:

@ECHO OFF
PUSHD .
FOR /R %%d IN (.) DO (
    cd "%%d"
    IF EXIST *.png (
       REN *.png *.jpg
    )
)
POPD

Update: I found a better solution here that you can run right from the command line (use %%f in stead of %f if using this inside a batch file):

FOR /R %f IN (*.png) DO REN "%f" *.jpg

Note that the above will process the current directory and its subdirectories. If necessary, you can specify an arbitrary directory as the root, like this:

FOR /R "D:\path\to\PNGs" %f IN (*.png) DO REN "%f" *.jpg
like image 116
Reinier Torenbeek Avatar answered Sep 17 '22 22:09

Reinier Torenbeek