使用批处理脚本重命名目录中的多个文件 [英] Rename multiple files in a directory using batch script

查看:104
本文介绍了使用批处理脚本重命名目录中的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大约有1000张图像,它们的名称类似"IMG-12223".我想将它们重命名为1 2 3 4 ...1000.我该怎么做.我写了一个批处理脚本,其中列出了文件,但是我不知道如何重命名每个文件.例如将名称为"IMG-12223"的第一张图像重命名为1,名称为"IMG-23441"的第二张图像重命名为2,依此类推...

I have about 1000 images and they have name like "IMG-12223". I want to rename them to 1 2 3 4 ... 1000. How can I do that. I have written a batch script which list the files but I don't know how to rename each file. e.g. rename first image with name "IMG-12223" to 1 , second image with name "IMG-23441" to 2 and so on ...

for /r %%i in (*) do (
    echo %c% 
)

推荐答案

这是脚本.只需将脚本放入您的文件夹中并运行它即可.

Here's the script. Just put the script in your folder and run it.

@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"等,则可以使用以下内容脚本.

If you want to keep the extensions, i.e. rename "IMG-12223.jpg", "IMG-12224.jpg", etc to "1.jpg", "2.jpg", etc, you may use the following script.

@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] Here're explanations for the lines mentioned in Jack's comment.

  • setlocal EnableDelayedExpansion

通常,我们希望变量a在执行时被延迟扩展,但不读取行.没有它,变量a不能获得其增加的值,而总是1.

In general, we want the variable a to be delayed expansion when it's executed but not the line is read. Without it, the variable a cannot get its increased value but always 1.

有关EnableDelayedExpansion的详细信息,请参阅答案 https://stackoverflow.com/a/18464353/2749114.

For the detail of EnableDelayedExpansion, please refer to the answer https://stackoverflow.com/a/18464353/2749114.

  • for /f "delims=" %%i in ('dir /b *.jpg')

dir中带有/b选项,仅列出所有jpg文件的文件名.

Here dir with /b option, lists only file names of all jpg files.

for循环遍历并重命名所有jpg文件.

The for loop traverses and renames all jpg files.

对于delims选项,由于默认的定界符字符是空格,如果没有选项delims=,则失败的原因是图像文件的文件名中带有空格. IE.对于名为带有spaces.jpg的img"的图像文件,如果没有该选项,则%%i的值为"img",而不是整个名称带有spaces.jpg的img",这是不正确的.

For the delims option, since the default delimiter character is a space, without the option delims=, it fails with the image files with spaces in the file names. I.E. for an image file named "img with spaces.jpg", without the option, the value of %%i is "img" but not the whole name "img with spaces.jpg", which is incorrect.

对于for循环,请参阅页面 http://ss64.com/nt/for_f .html .

For for loop, please refer to the page http://ss64.com/nt/for_f.html.

  • if not "%%~ni"=="%~n0"

为了更准确,我将其更改为if not "%%~nxi"=="%~nx0".并且随附的代码已更新.

I have change it to if not "%%~nxi"=="%~nx0" to be more accurate. And the codes attached have been updated.

它实际上是用来避免重命名bat文件本身.如果我们仅将重命名限制为"jpg"文件,则不需要该行.

It's actually used to avoid to rename the bat file itself. If we limit the renaming only upon "jpg" files, then the line is not needed.

%%~nxi是所遍历的每个文件的扩展名.而%~nx0是带有扩展名的正在运行的bat文件.有关详细信息,请参阅等同于Unix basename命令的DOS BAT文件?.

%%~nxi is the file name with extension for each file traversed. And %~nx0 is the running bat file with extension. For details, please refer to the page DOS BAT file equivalent to Unix basename command?.

这篇关于使用批处理脚本重命名目录中的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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