问题
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