增量重命名所有文本文件,而不会出现“重复”错误

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

给定文件

0.txt
1.txt
2.txt
等,我想识别现有的编号,并重命名该文件,将该编号增加
1

问题是我遇到的各种错误。重复、找不到文件、只有一些文件重命名正确等等..

@echo off
setlocal enabledelayedexpansion

for %%f in (*.txt) do (
    set "filename=%%~nf"
    set "extension=%%~xf"
    
    REM Check if the filename is a number and increment it
    if "!filename!" neq "" (
        set /a newNumber=!filename! + 1
        set "newFilename=!newNumber!!extension!"
        
        REM Check if the new filename already exists
        if exist "!newFilename!" (
            echo Duplicate name: "!newFilename!" already exists.
        ) else (
            ren "%%f" "!newFilename!"
        )
    )
)
echo
pause
endlocal

我期待这个:

  • 0.txt
    1.txt
  • 1.txt
    2.txt
  • 2.txt
    3.txt
  • 3.txt
    4.txt
  • 4.txt
    5.txt
  • 200.txt
    201.txt
batch-file cmd duplicates increment file-rename
1个回答
0
投票

好吧。我已经做到了。

@echo off
setlocal enabledelayedexpansion

rem Loop through existing .xhtml files in reverse order
for /L %%i in (478, -1, 0) do (
    if exist %%i.xhtml (
        set /a newnum=%%i + 1

        rem Attempt to rename the file
        ren %%i.xhtml !newnum!.xhtml
    )
)

endlocal

请务必注明文件夹中最大编号的名称。

© www.soinside.com 2019 - 2024. All rights reserved.