批处理:如何批量重命名文件(接近)?
我见过人们在 Perl 中这样做,但我想知道是否有办法通过批处理来做到这一点?它内置于 Windows 中,因此我认为了解如何使用批处理脚本来执行此操作会更有用。它不需要在计算机上安装任何东西。
输入名称示例:myFile_55
输出模式示例:将 myFile
更改为 picture
并将数字减少 13。 示例输出:picture_42
。
你会如何处理这个问题?我知道一个用于重命名的批处理命令:
ren myFile_55 picture_42
。
因此,如果我有一个名为 renamer.bat
的文件,我可以添加以下内容:
for /r %%x in (%1) do ren "%%x" %2
代码>.
然后我可以输入以下命令:
renamer.bat myfile* picture*
。
但我不知道如何减少数量。
I've seen people do this in Perl, but I'm wondering if there's a way to do it via batch? It's built into Windows, so I think it would be more useful to know how to do this with a batch script. It doesn't require installing anything onto a computer.
An example input name: myFile_55
An example output pattern: change myFile
to picture
and reduce the number by 13.
An example output: picture_42
.
How would you approach this? I know a batch command to rename:
ren myFile_55 picture_42
.
So, if I have a file named renamer.bat
, I can add the following:
for /r %%x in (%1) do ren "%%x" %2
.
Then I can type this command:
renamer.bat myfile* picture*
.
I don't know how to reduce the numbers, though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将原始文件名放入 for 循环中,并提取名称和数字,对数字进行数学运算,然后使用新的名称和数字将其重新插入。只要文件名格式为
name_number
,您就可以使用以下命令:编辑:编辑
REN
命令以包含原始文件的驱动器和路径。从小写 x 更改为大写 X,以免混淆%%~dpX
。You can probably put the original file name through a for loop, and extract the name and numbers, do the math on the numbers, then plug it back in with the new name and number. As long as the filename format is
name_number
you can use this:EDIT: Edited the
REN
command to include the drive and path of the original file. Changed from lower case x to upper case X as to not confuse%%~dpX
.