windows 如何在 PowerShell 中批量重命名文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13382638/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:03:46  来源:igfitidea点击:

How can I bulk rename files in PowerShell?

windowspowershell

提问by James Alexander

I'm trying to do the following:

我正在尝试执行以下操作:

Rename-Item c:\misc\*.xml *.tmp

I basically want to change the extension on every files within a directory to .tmpinstead of .xml. I can't seem to find a straight forward way to do this in PowerShell.

我基本上想将目录中每个文件的扩展名更改.tmp.xml. 我似乎无法在 PowerShell 中找到一种直接的方法来执行此操作。

回答by dugas

Derived from example 4 in the help documentation of Rename_Item retrieved with command:

源自使用命令检索的 Rename_Item 帮助文档中的示例 4:

get-help Rename-Item -examples

Example:

例子:

Get-ChildItem *.xml | Rename-Item -NewName { $_.Name -replace '\.xml','.tmp' }

Note the explanation in the help documentation for the escaping backslash in the replace command due to it using regular expressions to find the text to replace.

请注意帮助文档中对替换命令中转义反斜杠的解释,因为它使用正则表达式来查找要替换的文本。

回答by etoxin

This works well too when you're in the desired directory.

当您在所需的目录中时,这也很有效。

Dir | Rename-Item –NewName { $_.name –replace "old","new" }

回答by Ohad Schneider

The existing answers suggest the -replaceoperator, but what if the file is called a.xml.xml? Both .xmlsubstrings will be replaced and the end result would be a.tmp.tmp. Fortunately, there's a .NET method for this:

现有答案建议-replace操作员,但如果文件被调用a.xml.xml怎么办?两个.xml子字符串都将被替换,最终结果将是a.tmp.tmp. 幸运的是,这里有一个 .NET 方法:

Dir *.xml | rename-item -newname { [io.path]::ChangeExtension($_.name, ".tmp") } 

(Manish Kumarwas close with GetFileNameWithoutExtensionbut this is more elegant and probably a bit more efficient, not that it overly matters in this case)

Manish Kumar很接近,GetFileNameWithoutExtension但这更优雅,可能更有效率,但在这种情况下它并不重要)

回答by Trashman

Here's another variant that will work.

这是另一个可行的变体。

dir *.xml | Rename-Item -NewName {$_.BaseName + ".tmp"}

$_.BaseNamewill do the "base" name without the (last) extension.

$_.BaseName将使用没有(最后一个)扩展名的“基本”名称。

回答by spinalfrontier

a shortened version using the alias would be:

使用别名的缩短版本将是:

ls *.xml | ren -new {$_.BaseName + ".tmp"}

回答by John Smith

Even easier - remember that the replace search string is a regular expression,

更简单 - 请记住替换搜索字符串是一个正则表达式,

dir *.xml | rename-item -newname {$_.name -replace "xml$","tmp"}

The "$" represents end-of-string, so the characters "xml" must be the last three chars of the filename.

“$”代表字符串结尾,因此字符“xml”必须是文件名的最后三个字符。

回答by Manish Kumar

dir -Recurse | where-object -FilterScript {$_.Extension -eq ".xml"} | Rename-Item -NewName {[System.IO.Path]::GetFileNameWithoutExtension($_.fullname) + ".tmp"}

use -WhatIf to evaluate the result first

使用 -WhatIf 首先评估结果

回答by moonbase3

This seems to work and is a pythonic i.e simple is better than complex (https://www.python.org/dev/peps/pep-0020/) way of doing it (once you are in the directory):

这似乎有效并且是一种pythonic,即简单胜于复杂(https://www.python.org/dev/peps/pep-0020/)的方式(一旦您进入目录):

$files = Get-ChildItem -file -Filter *.xml;
  ForEach ($file in $files)
  {
  $n = $file.Basename
  Copy-Item -Path $file -Destination "$n.tmp"
  Remove-Item "$n.xml"
  }