How to Batch change file extensions within subfolders [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-12 07:49:31

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/15317647/how-to-batch-change-file-extensions-within-subfolders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!