这是脚本。只需将脚本放入您的文件夹中并运行它即可。
@echo off & setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *') do (
if not "%%~nxi"=="%~nx0" (
ren "%%i" "!a!"
set /a a+=1
)
)
如果您想保留扩展名,即将“IMG-12223.jpg”、“IMG-12224.jpg”等重命名为“1.jpg”、“2.jpg”等,您可以使用以下脚本。
@echo off & setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *.jpg') do (
ren "%%i" "!a!.jpg"
set /a a+=1
)
[Update] 这里是对杰克评论中提到的几行的解释。
setlocal EnableDelayedExpansion
一般来说,我们想要变量a
当执行但不读取该行时延迟扩展。没有它,变量a
无法获得其增加的值,但始终为 1。
关于EnableDelayedExpansion的详细内容请参考答案https://stackoverflow.com/a/18464353/2749114 https://stackoverflow.com/a/18464353/2749114.
for /f "delims=" %%i in ('dir /b *.jpg')
Here dir
with /b
选项,仅列出所有 jpg 文件的文件名。
The for
循环遍历并重命名所有jpg文件。
For the delims
选项,由于默认分隔符是空格,没有该选项delims=
,它会因文件名中带有空格的图像文件而失败。 IE。对于名为“img with space.jpg”的图像文件,如果没有该选项,则值为%%i
是“img”,但不是全名“img with space.jpg”,这是不正确的。
For for
循环,请参考页面http://ss64.com/nt/for_f.html http://ss64.com/nt/for_f.html.
我已将其更改为if not "%%~nxi"=="%~nx0"
更准确。并且所附代码已更新。
它实际上是用来避免重命名bat文件本身。如果我们限制仅对“jpg”文件进行重命名,则不需要该行。
%%~nxi
是遍历的每个文件的文件名及其扩展名。和%~nx0
是正在运行的bat文件,扩展名为.详情请参阅页面DOS BAT 文件相当于 Unix basename 命令? https://stackoverflow.com/questions/3432851/dos-bat-file-equivalent-to-unix-basename-command.