将数千个文件夹中的jpg文件重命名为avatar.jpg和main.jpg

时间:2021-08-31 00:35:03

I have thousands of folders with 2 jpg files in them..

我有成千上万的文件夹,里面有2个jpg文件..

For example the folder 1052 has:

例如,文件夹1052具有:

4a4df84f7e8d78100ceed89b368be78d.jpg
thumb_4a4df84f7e8d78100ceed89b368be78d.jpg

I want to rename the one that begins with thumb_ to avatar.jpg and the other one to main.jpg

我想将以thumb_开头的那个重命名为avatar.jpg,将另一个重命名为main.jpg

How to do this? Will cmd rename commands help or will I need to write a windows batch script ? If so what the the commands to be used ?

这该怎么做? cmd重命名命令会帮助还是我需要编写Windows批处理脚本?如果是这样,要使用的命令是什么?

I use Windows.

我用的是Windows。

2 个解决方案

#1


1  

Just figured out a trick.

刚想出一个技巧。

Rename everything with thumb to xyz extension

用拇指重命名所有内容到xyz扩展名

for /R %x in (thumb*.jpg) do ren "%x" avatar.xyz

Rename remaining job file to main.jpg:

将剩余的作业文件重命名为main.jpg:

for /R %x in (*.jpg) do ren "%x" main.jpg

Rename xyz back to jpg

将xyz重命名为jpg

for /R %x in (*.xyz) do ren "%x" *.jpg

#2


0  

You could do all in one shot:

你可以一次性完成所有工作:

rem // Iterate all `*.jpg` files:
for /R %%F in (*.jpg) do (
    rem /* Extract the part of the file name before the first `_`; the leading `-` is added
    rem    to avoid files like `__thumb___xxx.jpg` to be renamed to `avatar.jpg`: */
    for /F "delims=_ eol=_" %%I in ("-%%~nF") do (
        rem // Check the extracted file name part and rename the file accordingly:
        if /I "%%I"=="-thumb" (
            ren "%%~F" "avatar.*"
        ) else (
            ren "%%~F" "main.*"
        )
    )
)

To try this directly in command prompt, change all %% to %.

要在命令提示符下直接尝试此操作,请将所有%%更改为%。

#1


1  

Just figured out a trick.

刚想出一个技巧。

Rename everything with thumb to xyz extension

用拇指重命名所有内容到xyz扩展名

for /R %x in (thumb*.jpg) do ren "%x" avatar.xyz

Rename remaining job file to main.jpg:

将剩余的作业文件重命名为main.jpg:

for /R %x in (*.jpg) do ren "%x" main.jpg

Rename xyz back to jpg

将xyz重命名为jpg

for /R %x in (*.xyz) do ren "%x" *.jpg

#2


0  

You could do all in one shot:

你可以一次性完成所有工作:

rem // Iterate all `*.jpg` files:
for /R %%F in (*.jpg) do (
    rem /* Extract the part of the file name before the first `_`; the leading `-` is added
    rem    to avoid files like `__thumb___xxx.jpg` to be renamed to `avatar.jpg`: */
    for /F "delims=_ eol=_" %%I in ("-%%~nF") do (
        rem // Check the extracted file name part and rename the file accordingly:
        if /I "%%I"=="-thumb" (
            ren "%%~F" "avatar.*"
        ) else (
            ren "%%~F" "main.*"
        )
    )
)

To try this directly in command prompt, change all %% to %.

要在命令提示符下直接尝试此操作,请将所有%%更改为%。