How to write a batch file to rename files in numerically order within a particular folder?

限于喜欢 提交于 2021-01-28 20:50:45

问题


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

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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/31551660/how-to-write-a-batch-file-to-rename-files-in-numerically-order-within-a-particul

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!