使用计数器和特定文件名重命名文件的批处理文件

问题描述 投票:0回答:1

我正在尝试创建一个批处理文件,我可以将其复制并粘贴到任何文件夹中(我不想每次使用它时都必须更改目录路径),其中包含多个 jpg 文件,这样当我运行时.bat 文件,它将使用特定标题重命名该文件夹中的所有 jpg 文件名,使 jpg 文件保持与原来相同的顺序。我希望将文件夹中的第一个 jpg 重命名为 1 - Front,然后下一个 jpg 重命名为 2 - Back,然后下一个 jpg 重命名为 3 - Left Side,然后下一个 jpg 重命名为 4 - 右侧,然后下一个 jpg 重命名为 5 - 顶部,然后下一个 jpg 重命名为 6 - 底部

我已经尝试了 4 个脚本。第一个:

@echo off
setlocal enabledelayedexpansion

REM Get the current directory where the batch file is located
set "folderPath=%~dp0"

REM Initialize the counter
set "counter=1"

REM Define the desired filenames
set "filenames=Front Back Left Side Right Side Top Bottom"

REM Loop through each JPG file in the folder
for %%F in ("%folderPath%\*.jpg") do (
    REM Rename the file based on the counter and filenames
    for %%A in (!counter!) do (
        ren "%%F" "%%A - !filenames:~%%A,1!.jpg"
    )
    REM Increment the counter
    set /a "counter+=1" 
)

这会将第一个文件重命名为 1 - r,第二个文件重命名为 2 - o,第三个文件重命名为 3 - n,第四个文件重命名为 4 - t,第五个文件重命名为 5 -,第六个文件重命名为 6 -

我也尝试过这个:

@echo off
setlocal enabledelayedexpansion

REM Get the current directory where the batch file is located
set "folderPath=%~dp0"

REM Initialize the counter
set "counter=1"

REM Define the desired filenames
set "filenames=Front Back Left Side Right Side Top Bottom"

REM Loop through each JPG file in the folder
for %%F in ("%folderPath%\*.jpg") do (
    REM Get the corresponding filename from the list
    for %%A in (!counter!) do (
        for %%B in (!filenames!) do (
            if %%A==1 (
                ren "%%F" "%%A - %%B.jpg"
            ) else (
                REM Increment the counter
                set /a "counter+=1"
                if %%A==2 (
                    ren "%%F" "%%A - %%B.jpg"
                ) else (
                    REM Increment the counter
                    set /a "counter+=1"
                    if %%A==3 (
                        ren "%%F" "%%A - %%B.jpg"
                    ) else (
                        REM Increment the counter
                        set /a "counter+=1"
                        if %%A==4 (
                            ren "%%F" "%%A - %%B.jpg"
                        ) else (
                            REM Increment the counter
                            set /a "counter+=1"
                            if %%A==5 (
                                ren "%%F" "%%A - %%B.jpg"
                            ) else (
                                REM Increment the counter
                                set /a "counter+=1"
                                if %%A==6 (
                                    ren "%%F" "%%A - %%B.jpg"
                                )
                            )
                        )
                    )
                )
            )
        )
    )
)

这会将文件重命名为 1 - Back、1 - Front、1 - Left、1 - Right、1 - Side、1 - Top。

接下来,我尝试了:

@echo off
setlocal enabledelayedexpansion

REM Get the current directory where the batch file is located
set "folderPath=%~dp0"

REM Initialize the counter
set "counter=1"

REM Define the desired filenames
set "filenames=Front Back Left Side Right Side Top Bottom"

REM Loop through each JPG file in the folder
for %%F in ("%folderPath%\*.jpg") do (
    REM Rename the file based on the counter and filenames
    for %%A in (!counter!) do (
        for %%B in (!filenames!) do (
            if %%A==%%B (
                ren "%%F" "%%A.jpg"
            )
        )
    )
    REM Increment the counter
    set /a "counter+=1" 
)

最后,我尝试了:

@echo off
setlocal enabledelayedexpansion

REM Get the current directory where the batch file is located
set "folderPath=%~dp0"

REM Define the desired filenames
set "filenames=Front Back Left Side Right Side Top Bottom"

REM Initialize the counter
set "counter=1"

REM Loop through each JPG file in the folder
for %%F in ("%folderPath%\*.jpg") do (
    REM Extract the corresponding filename from the list
    for %%A in (!counter!) do (
        for %%B in (!filenames!) do (
            if %%A==%%B (
                set "newName=%%B.jpg"
                ren "%%F" "!newName!"
            )
        )
    )
    REM Increment the counter
    set /a "counter+=1" 
)

最后两个都没有做任何事情。有人可以帮忙吗?谢谢!

batch-file batch-rename
1个回答
0
投票

这只是您可以做到这一点的一种可能的方式。将新文件名输出到临时文件。然后将其用作代码块的标准输入。这将被

SET /P
命令捕获。使用
FOR /F
命令捕获
dir
命令的输出。与普通的
FOR
命令相比,使用此命令至关重要。在执行之前,FOR /F 会枚举所有文件。使用普通的 FOR 命令时,它一次处理一个文件,这将导致文件重新进入队列时出现问题。我只是重复重命名,向您展示它会做什么。如果看起来正确,只需从重命名命令中删除回显即可。

@echo off
setlocal enabledelayedexpansion
REM List JPG files in directory
dir /a-d /b *.jpg
(
echo 1 - Front
echo 2 - Back
echo 3 - Left
echo 4 - Right Side
echo 5 - Top
echo 6 - Bottom
)>tmp.txt

<tmp.txt (FOR /F "delims=" %%G IN ('dir /a-d /b *.jpg') DO (
    set /p newname=
    echo rename "%%G" "!newname!%%~xG"
    )
)
del tmp.txt

这是执行过程。

D:\My Drive\BatchFiles\SO\78206913>so.bat
purple.jpg
pink.jpg
foo.jpg
bar.jpg
green.jpg
red.jpg
rename "purple.jpg" "1 - Front.jpg"
rename "pink.jpg" "2 - Back.jpg"
rename "foo.jpg" "3 - Left.jpg"
rename "bar.jpg" "4 - Right Side.jpg"
rename "green.jpg" "5 - Top.jpg"
rename "red.jpg" "6 - Bottom.jpg"
© www.soinside.com 2019 - 2024. All rights reserved.