使用PowerShell重命名文件时,如何使用"["处理文件名? [英] When renaming a file with PowerShell, how do I handle filenames with "["?
问题描述
我正在运行以下PowerShell命令:
I'm running this PowerShell command:
Get-ChildItem .\tx\*.htm | Rename-Item -NewName {$_.Name -replace '\.htm','.tmp'}
并在文件名包含方括号-[
和/或]
时收到以下错误,这是可以理解的,因为它们在PowerShell语法中具有含义.
and receive the following error when a filename contains square brackets -- [
and/or ]
--, understandable since those have a meaning within the PowerShell syntax.
Rename-Item : Cannot rename because item at
'Microsoft.PowerShell.Core\FileSystem::C:\users\xxxxx\desktop\tx\
Foofoofoofoo_foo_foo_[BAR]_Foofoofoofoo_foofoofoo.htm' does not exist.
At C:\users\xxxxx\desktop\foo002.ps1:59 char:39
+ Get-ChildItem .\tx\*.htm | Rename-Item <<<< -NewName { $_.Name -replace '\.htm','.tmp' }
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
路径中的所有其他文件都具有相似的名称(Phrase_With_Underscores.htm
),并且被重命名而没有发生问题.任何人对此都有任何经验,并且知道如何解决它,以便我可以操纵这些文件?
All the other files in the path have similar names (Phrase_With_Underscores.htm
), and are renamed without incident. Anyone have any experience with this and know how to counter it so I can manipulate these files?
推荐答案
您可以使用move-item
解决方法,以使用参数-LiteralPath
重命名项目.
You can workaround using move-item
for renaming items using the parameter -LiteralPath
.
Is a know bug: Connect (read Keith Hill Comment for: powershell V3 fix this issue)
这篇关于使用PowerShell重命名文件时,如何使用"["处理文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!