使用 Windows 批处理脚本重命名目录中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9383032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename all files in a directory with a Windows batch script
提问by Blainer
How would I write a batch or cmd file that will rename all files in a directory? I am using Windows.
我将如何编写一个批处理或 cmd 文件来重命名目录中的所有文件?我正在使用 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
回答by dbenham
A FOR statement to loop through the names (type FOR /?
for help), and string search and replace (type SET /?
for help).
FOR 语句循环遍历名称(输入FOR /?
帮助),以及字符串搜索和替换(输入SET /?
帮助)。
@echo off
setlocal enableDelayedExpansion
for %%F in (*120x90.jpg) do (
set "name=%%F"
ren "!name!" "!name:120x90=67x100!"
)
UPDATE - 2012-11-07
更新 - 2012-11-07
I've investigated how the RENAME command deals with wildcards: How does the Windows RENAME command interpret wildcards?
我研究了 RENAME 命令如何处理通配符:Windows RENAME 命令如何解释通配符?
It turns out that this particular problem can be very easily solved using the RENAME command without any need for a batch script.
事实证明,使用 RENAME 命令可以非常轻松地解决这个特定问题,而无需任何批处理脚本。
ren *_120x90.jpg *_67x100.*
The number of characters after the _
does not matter. The rename would still work properly if 120x90
became x
or xxxxxxxxxx
. The important aspect of this problem is that the entire text between the last _
and the .
is replaced.
后面的字符数_
无关紧要。如果120x90
变成x
或,重命名仍然可以正常工作xxxxxxxxxx
。这个问题的重要方面是替换last_
和 the之间的整个文本.
。
回答by Iain Samuel McLean Elder
As of Windows 7 you can do this in one line of PowerShell.
从 Windows 7 开始,您可以在一行 PowerShell 中执行此操作。
powershell -C "gci | % {rni $_.Name ($_.Name -replace '120x90', '67x100')}"
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
.
powershell -C "..."
启动 PowerShell 会话以运行引用的命令。当命令完成时它返回到外壳。-C
是 的缩写-Command
。
gci
returns all the files in the current directory. It is an alias for Get-ChildItem
.
gci
返回当前目录中的所有文件。它是 的别名Get-ChildItem
。
| % {...}
makes a pipeline to process each file. %
is an alias for Foreach-Object
.
| % {...}
制作一个管道来处理每个文件。%
是 的别名Foreach-Object
。
$_.Name
is the name of the current file in the pipeline.
$_.Name
是管道中当前文件的名称。
($_.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.
($_.Name -replace '120x90', '67x100')
使用-replace
运算符创建新文件名。第一个子串的每次出现都被第二个子串替换。
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.
rni
更改每个文件的名称。第一个参数(称为-Path
)标识文件。第二个参数(称为-NewName
)指定新名称。rni
是Rename-Item的别名。
Example
例子
$ dir
Volume in drive C has no label.
Volume Serial Number is A817-E7CA
Directory of C:\fakedir\test
11/09/2013 16:57 <DIR> .
11/09/2013 16:57 <DIR> ..
11/09/2013 16:56 0 750_MOT_Forgiving_120x90.jpg
11/09/2013 16:57 0 751_MOT_Persecution_1_120x90.jpg
11/09/2013 16:57 0 752_MOT_Persecution_2_120x90.jpg
3 File(s) 0 bytes
2 Dir(s) 243,816,271,872 bytes free
$ powershell -C "gci | % {rni $_.Name ($_.Name -replace '120x90', '67x100')}"
$ dir
Volume in drive C has no label.
Volume Serial Number is A817-E7CA
Directory of C:\fakedir\test
11/09/2013 16:57 <DIR> .
11/09/2013 16:57 <DIR> ..
11/09/2013 16:56 0 750_MOT_Forgiving_67x100.jpg
11/09/2013 16:57 0 751_MOT_Persecution_1_67x100.jpg
11/09/2013 16:57 0 752_MOT_Persecution_2_67x100.jpg
3 File(s) 0 bytes
2 Dir(s) 243,816,271,872 bytes free