在cmd和batch中使用数字序列重命名多个文件

0 command cmd batch-file command-prompt

这是我在这个美丽的网站上提出的第一个问题。正如您可能在标题中读到的那样,我想重命名可变数量的文件,在cmd和批处理文件中使用一系列数字,该序列是递增的,就像这样(1,2,3,4,5, 6、7、8、9、10...)。例如:

Test.txt它应该变成1.txt

Another.txt应变为2.txt

等等,一切都是自动的。

我的想法是设置一个类似的变量set /a number=1,并通过循环向其添加+1 set number="%number%+1",并每次重命名,但这是不可能的,因为当我使用ren 命令重命名文件时,它会立即重命名。

谁能帮我提供cmd和批处理文件版本?

提前致谢

Moh*_*iya 6

这应该对你有用。

set /a Index=1

setlocal enabledelayedexpansion

for /r %%i in (*.txt) do ( 
    rename "%%i" "!Index!.txt"
    set /a Index+=1
)
Run Code Online (Sandbox Code Playgroud)

使用上述代码创建一个批处理文件,并在 .txt 文件所在的文件夹中运行它。

如果你想追加“0”使其成为2位数字。您可以尝试添加 if else 语句,如下所示。

set /a Index=1

setlocal enabledelayedexpansion

for /r %%i in (*.txt) do ( 
    rem if number is less than 10, append 9 to file name
    if !Index! lss 10 (
        rename "%%i" 0"!Index!.txt"
    ) else (
        rename "%%i" "!Index!.txt"
    )
    
    set /a Index+=1
)
Run Code Online (Sandbox Code Playgroud)