10

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.

Andriy M
  • 71,352
  • 17
  • 87
  • 142
Jai Burrell
  • 101
  • 1
  • 3
  • 6
  • I think this question belongs in [SuperUser](http://superuser.com/) rather than StackOverflow. Stack Overflow is dedicated for programming questions. – Jesse Mar 10 '13 at 01:02
  • 1
    @Jesse: There are many questions on SO about batch or other shell programming, or similarly, bash programming. Why would this one in particular not fit here? – Reinier Torenbeek Mar 10 '13 at 01:14
  • @Jesse: Indeed, this is about scripting, not just using a ready-made tool. Scripting is programming too, especially when it requires control structures, like loops. – Andriy M Mar 10 '13 at 01:31

1 Answers1

17

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
Community
  • 1
  • 1
Reinier Torenbeek
  • 14,439
  • 5
  • 37
  • 57
  • 1
    `pushd .`? What's the point (if you pardon the pun)? :) – Andriy M Mar 10 '13 at 01:27
  • @AndriyM: Funny pun :-) I wanted to make sure that the command prompt returns to the original directory after completing in all subdirectories, and not end up in the directory it last `cd`-ed into. Is there a better way to achieve this? – Reinier Torenbeek Mar 10 '13 at 01:30
  • An *alternative* way, yes. Whether it would be better, I'm not sure. What I have in mind is storing `%CD%` in a variable before the loop, then CD-ing to it afterwards. Two commands as well, but too banal, I guess. :) Your explanation makes perfect sense to me, thanks for that. – Andriy M Mar 10 '13 at 01:35
  • Although you could probably just forego changing the directory all together. I think `ren "%%d\*.png" *.jpg` should work just fine. (You could also omit `if exist` as well: `ren ... 2>nul` would hide warnings about non-existent files.) – Andriy M Mar 10 '13 at 01:39
  • @AndriyM: Good suggestion, thanks, that is an improvement indeed. I found an even better alternative on SO -- see the updated answer. – Reinier Torenbeek Mar 10 '13 at 02:01
  • Yes, that's a very nice and elegant one-liner! Renaming files one by one may be slower than renaming them in bunches, though. – Andriy M Mar 10 '13 at 02:14
  • Thankyou so very much Reinier Torenbeek - you are a lifesaver, greatly appreciated – Jai Burrell Mar 10 '13 at 07:18
  • My pleasure, and welcome to StackOverflow. Since you are new here, please make sure to read the [FAQ](http://stackoverflow.com/faq), in particular the section about [how to ask questions](http://stackoverflow.com/faq#howtoask) concerning upvoting and accepting answers. – Reinier Torenbeek Mar 10 '13 at 19:59