windows - 在 Windows 中为文件名添加数字后缀

标签 windows batch-file cmd filenames

我想将大量文件重命名为 S001S002 等。 我从 here 得到这段代码添加前缀:

for %a in (*.*) do ren "%a" "prefix - %a"

所以我尝试修改它,使变量随着 for 递增,如下所示:

for /l %x in (1,1,5) do %a in (*.*) do ren "%a" "SQ00%x"

显然这是错误的,我后来读到 /l 可以用数值而不是文件名来工作,所以 %a 没有任何意义(如果我我错了)。

所以我想知道如何使用文件夹内文件的文件名在 for 循环中增加一个值。

编辑

我现在设法编写了一个重命名文件的批处理,并且:

  • 在特定目录下做
  • 具有特定的文件扩展名
  • 告诉您该目录中有多少该扩展名的文件
  • 请求一个新名称,稍后会添加数字后缀
  • 循环更改不同目录中的文件

    ECHO OFF
    
    Setlocal enabledelayedexpansion
    
    :start
    
    cls
    
    echo "%cd%"
    
    echo where are the files?        
    set/p "files=>"        
    cd "%files%"
    
    echo now in:    
    echo "%cd%"
    
    echo files extension:
    set/p "ext=>"
    
    ::amount of files in the directory
    set/a cnt=0    
    for %%a in (*.%ext%) do set /a cnt+=1
    echo File count = %cnt%
    PAUSE
    
    echo new file name:
    set/p "name=>"
    
    set/a Count=1
    for /r %%a in (*.%ext%) do (
    echo !Count!
    ren "%%a" "%name%!Count!.%ext%"
    set /a Count=Count+1
    )
    
    PAUSE
    
    goto start
    

现在的问题是,重命名文件的 FOR 循环比目录中的文件数量多了 1 个循环,我不知道为什么

run

最佳答案

Setlocal enabledelayedexpansion
for %%a in (*.*) do (
    set /a Count=Count+1
    echo ren "%%a" "!Count! - %%a"
    )

是一种方式。请参阅 set/?setlocal/?

从 Windows NT 命令外壳

Parentheses can also be used to enter multi-line commands. If a command line ends with one or more sets of unbalanced parentheses, the command line is assumed to continue on the next line. If the command was entered interactively, the shell prompts for more input until all parentheses balance. If the command is part of a script, the shell reads additional script lines until all the parentheses balance. For example:

1. C:\>(
2. More?echo command1
3. More?echo command2
4. More?)
5. command1
6. command2

The first line consists only of an open parenthesis. The shell detects this, and prompts for more input. Next, two ECHO commands are entered. Finally, a closing parenthesis balances the opening parenthesis and the command is complete. The shell then executes the compound command, which executes the two individual ECHO commands. Individual commands do not span lines in multi-line commands. The end of a physical line always terminates a simple command (either as typed or as entered in a script file). Notice in the preceding example how the end of the physical line terminated each ECHO command.

关于windows - 在 Windows 中为文件名添加数字后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388240/

相关文章:

node.js - 为什么 npm install -g @angular/cli?

windows - 用于检查域中计算机名称的简单批处理脚本

python - 无法启动 Windows 快捷方式

powershell - 使用启动过程调用其他Powershell文件时出现问题

batch-file - Echo 在批处理文件中写入尾随空格

batch-file - 将 IP 地址写入文件

batch-file - 使用 cmd 一次重命名多个文件 [Windows 10]

windows - NSIS - 强制用户选择不同的目录

python 桑巴 : user credentials not passed (cli_init_creds)?

batch-file - Windows cmd FORFILES 不适用于批处理文件,但适用于命令提示符