命令提示符/批处理-使用顺序编号重命名多个文件 [英] Command Prompt/Batch - rename multiple files with sequential numbering

查看:445
本文介绍了命令提示符/批处理-使用顺序编号重命名多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有多个文件

filename.a.txt
filename.b.txt
filename.c.txt

我想运行一个以所有.txt文件为目标的批处理文件,并将它们重命名为我在自定义%filename%var中设置的名称,再给它们编号,以便最终变成:

I want to run a batch file that targets all .txt files and rename them to whatever I have set into my custom %filename% var + give them numbers so it would end up into something like:

filename.1.txt
filename.2.txt
filename.3.txt

到目前为止,我已经写了这个:

So far I have wrote this:

set filename=FileTitle
for /r %%i in (*.txt) do call ren %%i %filename%.txt

它可以工作,但是问题在于它只是选择了第一个.txt文件,并为其提供了FileTitle文件名,仅此而已.我无法弄清楚如何批量重命名所有.txt文件并为它们提供唯一的序列号作为输出的%filename%.txt的前缀/后缀/自定义变量,例如%filename%-%uniquesuffix%.txt.所以我需要设置某种变量,为每个文件赋予唯一的编号,例如从1-99开始,按字母顺序(cmd提示拾取文件的默认顺序).

And it works, but problem is that it just picks up first .txt file and gives it the FileTitle filename and that's it. I can't figure how to rename all .txt files in a batch and give them unique sequential number as a prefix/suffix/custom var to the outputed %filename%.txt so something like e.g. %filename%-%uniquesuffix%.txt. So I need to set some kind of variable that gives each file a unique number e.g. from 1-99 in alphabet order (default order that cmd prompt picked files up).

我确实搜索了其他答案,但它们仅显示了如何为重命名文件添加全局/相同前缀.

I did search other answers, but they show only how to add global/same prefix to renamed files.

推荐答案

@echo off
setlocal EnableDelayedExpansion

set filename=FileTitle
set suffix=100
for /F "delims=" %%i in ('dir /B *.txt') do (
   set /A suffix+=1
   ren "%%i" "%filename%-!suffix:~1!.txt"
)

此代码以您请求的形式重命名文件.请注意,数字后缀具有两位数字,以便保留文件的原始字母顺序.如果使用了自然" 1-99数字,则该cmd提示中显示文件的顺序将以这种方式更改:1.txt 10.txt 11.txt ... 19.txt 2.txt 20.txt 21.txt ... 29.txt 3.txt ...(字母顺序,不是数字1).如果可能超过99个文件,只需在set suffix=100命令中添加另一个零以生成三位数的后缀即可.

This code renames the files in the form you requested. Note that the numeric suffix have two digits in order to preserve the original alphabet order of the files. If "natural" 1-99 numbers were used, then the order in that cmd prompt show the files is changed in this way: 1.txt 10.txt 11.txt ... 19.txt 2.txt 20.txt 21.txt ... 29.txt 3.txt ... (alphabet order, NOT numeric one). If may be more than 99 files, just add another zero in set suffix=100 command to generate a three-digits suffix.

还请注意,根据将重命名文件放置在原始文件列表中的站点,普通的for命令可能会两次或多次处理某些文件.为避免此问题,请在dir /B *.txt上使用for /F命令.此方法 first 首先获取所有文件的列表,然后然后在这样的静态列表上执行重命名.

Note also that your plain for command may process some files twice or more times depending on the site where the renamed files will be placed in the original files list. To avoid this problem use a for /F command over a dir /B *.txt one. This method first get a list of all files, and then execute the renames over such a static list.

要处理当前文件夹下的所有文件(这是for命令中的/r开关的目的),只需在dir命令中添加/S开关.

To process all files under current folder (that is the purpose of your /r switch in for command), just add a /S switch in the dir command.

这篇关于命令提示符/批处理-使用顺序编号重命名多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆