You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批处理脚本需求:按指定规则批量重命名JPG文件

批量重命名JPG文件为指定序列名称的批处理解决方案

我需要一个可复制到任意含JPG文件的文件夹中运行的批处理文件,无需修改路径。要求按文件原顺序将JPG依次重命名为:1 - Front.jpg、2 - Back.jpg、3 - Left Side.jpg、4 - Right Side.jpg、5 - Top.jpg、6 - Bottom.jpg。我尝试了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.jpg、2 - o.jpg这类仅提取单个字符的名称,因为!filenames:~%%A,1!是按字符位置截取,而非按空格分隔的条目获取。


第二个脚本

@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.jpg、1 - Front.jpg,因为内层循环遍历所有文件名条目,每次循环都会执行重命名,导致单个原文件被多次重命名。


第三、第四个脚本

@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" 
)

问题:完全没有效果,因为逻辑错误——尝试将数字计数器%%A和文件名条目%%B进行相等判断,显然永远不会成立,因此不会执行任何重命名操作。


正确的批处理脚本

解决思路是将目标名称存储为数组,遍历JPG文件时按顺序取出数组中的对应条目进行重命名:

@echo off
setlocal enabledelayedexpansion

REM 定义目标名称数组,索引从1开始
set "name[1]=Front"
set "name[2]=Back"
set "name[3]=Left Side"
set "name[4]=Right Side"
set "name[5]=Top"
set "name[6]=Bottom"

set "counter=1"

REM 遍历当前文件夹下的所有JPG文件(按系统默认顺序)
for %%F in ("%~dp0*.jpg") do (
    REM 检查计数器是否在1-6范围内,避免超出条目数
    if !counter! leq 6 (
        ren "%%F" "!counter! - !name[!counter!]!.jpg"
    )
    set /a counter+=1
)

echo 重命名完成!
pause

脚本说明

  1. 使用数组存储目标名称,避免空格分隔带来的拆分问题,尤其是Left Side这类包含空格的名称。
  2. %~dp0直接指向批处理文件所在的文件夹,无需额外设置路径变量,复制到目标文件夹即可运行。
  3. 添加了范围判断if !counter! leq 6,防止文件夹中JPG数量超过6个时出现无对应名称的情况。
  4. 最后添加提示和暂停,方便查看执行结果。

内容的提问来源于stack exchange,提问作者user23510471

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 17:58:11