使用 Windows 批处理脚本重命名目录中的所有文件
我将如何编写一个批处理或cmd文件来重命名目录中的所有文件?我正在使用Windows。
改变这个:
750_MOT_Forgiving_120x90.jpg
751_MOT_Persecution_1_120x90.jpg
752_MOT_Persecution_2_120x90.jpg
753_MOT_Hatred_120x90.jpg
754_MOT_Suffering_120x90.jpg
755_MOT_Freedom_of_Religion_120x90.jpg
756_MOT_Layla_Testimony_1_120x90.jpg
757_MOT_Layla_Testimony_2_120x90.jpg
到这个:
750_MOT_Forgiving_67x100.jpg
751_MOT_Persecution_1_67x100.jpg
752_MOT_Persecution_2_67x100.jpg
753_MOT_Hatred_67x100.jpg
754_MOT_Suffering_67x100.jpg
755_MOT_Freedom_of_Religion_67x100.jpg
756_MOT_Layla_Testimony_1_67x100.jpg
757_MOT_Layla_Testimony_2_67x100.jpg
How would I write a batch or cmd file that will rename all files in a directory? I am using Windows.
Change this:
750_MOT_Forgiving_120x90.jpg
751_MOT_Persecution_1_120x90.jpg
752_MOT_Persecution_2_120x90.jpg
753_MOT_Hatred_120x90.jpg
754_MOT_Suffering_120x90.jpg
755_MOT_Freedom_of_Religion_120x90.jpg
756_MOT_Layla_Testimony_1_120x90.jpg
757_MOT_Layla_Testimony_2_120x90.jpg
To this:
750_MOT_Forgiving_67x100.jpg
751_MOT_Persecution_1_67x100.jpg
752_MOT_Persecution_2_67x100.jpg
753_MOT_Hatred_67x100.jpg
754_MOT_Suffering_67x100.jpg
755_MOT_Freedom_of_Religion_67x100.jpg
756_MOT_Layla_Testimony_1_67x100.jpg
757_MOT_Layla_Testimony_2_67x100.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用于循环遍历名称的 FOR 语句(键入
FOR /?
获取帮助)以及字符串搜索和替换(键入SET /?
获取帮助)。更新 - 2012-11-07
我研究了 RENAME 命令如何处理通配符:如何Windows RENAME 命令会解释通配符吗?
事实证明,使用 RENAME 命令可以非常轻松地解决这个特定问题,而无需任何批处理脚本。
_
后面的字符数无关紧要。如果120x90
变为x
或xxxxxxxxxx
,重命名仍然可以正常工作。此问题的重要方面是最后一个_
和.
之间的整个文本都被替换。A FOR statement to loop through the names (type
FOR /?
for help), and string search and replace (typeSET /?
for help).UPDATE - 2012-11-07
I've investigated how the RENAME command deals with wildcards: How does the Windows RENAME command interpret wildcards?
It turns out that this particular problem can be very easily solved using the RENAME command without any need for a batch script.
The number of characters after the
_
does not matter. The rename would still work properly if120x90
becamex
orxxxxxxxxxx
. The important aspect of this problem is that the entire text between the last_
and the.
is replaced.从 Windows 7 开始,您可以通过一行 PowerShell 完成此操作。
说明
powershell -C "..."
启动 PowerShell 会话来运行引用的命令。命令完成后,它返回外壳。-C
是-Command
的缩写。gci
返回当前目录中的所有文件。它是Get-ChildItem
的别名。<代码>| % {...} 创建一个管道来处理每个文件。
%
是Foreach-Object< 的别名/代码>
。
$_.Name
是管道中当前文件的名称。($_.Name -replace '120x90', '67x100')
使用-replace
运算符来创建新文件名。第一个子字符串的每次出现都被替换为第二个子字符串。rni
更改每个文件的名称。第一个参数(称为-Path
)标识文件。第二个参数(称为-NewName
)指定新名称。rni
是 Rename-Item 的别名。例子
As of Windows 7 you can do this in one line of PowerShell.
Explanation
powershell -C "..."
launches a PowerShell session to run the quoted command. It returns to the outer shell when the command completes.-C
is short for-Command
.gci
returns all the files in the current directory. It is an alias forGet-ChildItem
.| % {...}
makes a pipeline to process each file.%
is an alias forForeach-Object
.$_.Name
is the name of the current file in the pipeline.($_.Name -replace '120x90', '67x100')
uses the-replace
operator to create the new file name. Each occurrence of the first substring is replaced with the second substring.rni
changes the name of each file. The first parameter (called-Path
) identifies the file. The second parameter (called-NewName
) specifies the new name.rni
is an alias for Rename-Item.Example
我发现这个批处理脚本非常有用(见下文),它基本上是 dbenham 的 解决方案:
您只需另存为 .bat 文件,然后在包含要重命名的文件的文件夹中运行它即可。首先,您切换出搜索文本并输入替换文本并在运行之前保存它。然后它只会重命名那些包含输入字符串的文件(现在是“searchtext”)。像这样使用通配符(即
*%搜索%*
),该字符串可以位于名称中的任何位置。
不过,此版本还将扩展名替换为输入的扩展名 (.jpg)。对于我自己的使用,这就是我所寻找的,但如果您不想更改扩展名,则 dbenham 的版本使用“名称”而不是“文件名”,并结合删除有关扩展名的部分会更好。
我使用不同的文件(包括带有下划线的文件)运行了几次,到目前为止它按预期工作。
I found this batch script quite useful (see below), which is basically a variation on dbenham's solution:
You just save as a .bat file and run it in the folder with the files you want to rename. First you switch out the search text and enter the replacement text and save it before running. Then it will only rename those files that contain the entered string (now "searchtext"). Using the wild-cards like this (i.e.
*%search%*
), the string can be anywhere in the name.
However, this version also replaces the extension to the entered extension name (.jpg). For my own use that was what I was looking for, but if you don't want to change the extension, dbenham's version with "name" instead of "filename" in combination with deleting the parts about the extension will work better.
I ran it a couple of times with different files, including ones with underscores, and it worked as intended so far.