如何编写批处理文件以按数字顺序重命名特定文件夹中的文件? [英] How to write a batch file to rename files in numerically order within a particular folder?

查看:76
本文介绍了如何编写批处理文件以按数字顺序重命名特定文件夹中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三张图片的文件,它们分别命名为ape.jpg,123.jpg,zoo.jpg我想编写一个批处理文件,它可以将所有三张图片重命名为数字顺序.

I have a file contain three pictures, they named ape.jpg, 123.jpg, zoo.jpg I want to write a batch file, it can rename all three pictures into a numerical order.

ape.jpg -> (1).jpg
123.jpg -> (2).jpg
zoo.jpg -> (3).jpg

我不太在意这些图片的原始名称和顺序,我知道可以在任何版本的Windows中手动输入该文件夹并将其全部选中,将其中一个重命名为(1),其余图片将按照数字顺序排列.但是我想用批处理文件来做到这一点.

I don't really care about the original names and orders of those pictures, I know this can be done in any version of windows by manually go into that folder and select them all, rename one of them to (1), and rest of pictures will order themself numerically. but I want to do this with batch file.

推荐答案

您应该能够使用FOR循环和计数器变量来执行此操作. for 将遍历 dir 的输出以获得完整的文件列表,然后顺序重命名它们.

You should be able to do this with a FOR loop and a counter variable. The for will loop over the output of dir to get a full list of files, and then rename them sequentially.

setlocal enabledelayedexpansion
set i=1
for /F %%a in ('dir /B *.jpg') do (
  echo ren "%%a" "(!i!).jpg"
  set /a i+=1
)
endlocal

这不能保证它们的显示顺序,但是它应该在当前目录中的所有* .jpg文件上运行.运行此命令,它将向您显示它将使用的重命名命令.要使其实际重命名文件,请在带有 ren 的行之前删除 echo .

This makes no guarantee about what order they appear in, but it should operate on all *.jpg files in the current directory. Run this and it will show you the rename commands that it would use. To make it actually rename the files, remove the echo before the line with ren.

这篇关于如何编写批处理文件以按数字顺序重命名特定文件夹中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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