如何批量重命名文件为文件夹名+递增数字?我收到重复名称错误

How do I rename files to folder name + incremented numbers in batch? I'm getting Duplicate name error

我一直在尝试创建批处理文件以将我的文件重命名为:

"Foldername_01.jpg, Foldername_02.jpg, etc..."

但我的输出将第一个文件重命名为“%~dp00.jpg”,其余的 returns 错误

"A duplicate file name exists, or the file cannot be found"

我的代码:

@echo off
set /a count = 0

for %%a in (*.jpg) do (
    set /a count+=1
    ren "%%a" "%%~dp0_%count%.jpg" 
    )

pause

我不明白为什么计数变量没有从每个条目中加 1,并且文件被重命名为“%~dp0 + count + .ext”而不是 "folder name + count + .ext"

你的问题可以通过使用延迟扩展来处理,你的递增变量,在嵌套的 for 循环中,捕获目录名称.

示例:

@Set "count=0"
@For /F "Delims=" %%I In ('Dir /B/S/A-D-S "*.jpg" 2^>NUL')Do @(
    For %%J In ("%%~pI.")Do @(Set /A count+=1
        SetLocal EnableDelayedExpansion
        Ren "%%I" "%%~nxJ_!count!%%~xI"
        EndLocal))
@Pause