使用 Powershell 替换方括号 [英] Replace square bracket using Powershell
本文介绍了使用 Powershell 替换方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果您有一个文件名,例如委员会会议记录 [2010 年 10 月] - hq.doc",您如何让 Powershell 替换方括号?以下不起作用:
If you have a filename such as "Committee minutes [October 2010] - hq.doc", how do you get Powershell to replace the square brackets? The following doesn't work:
ls -filter *`[*`]* | foreach -Process { Rename-Item $_ -NewName ($_.Name -replace '\[', '\(') | Rename-Item $_ -NewName ($_.Name -replace '\]', '\)')}
我收到错误:
Rename-Item : Cannot rename because item at 'Committee minutes [October 2010] - hq.doc' does not exist.
At line:1 char:53
+ ls -filter *`[*`]* | foreach -Process { Rename-Item <<<< $_ -NewName ($_.Name -replace '\['
]', '\)')}
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
推荐答案
不幸的是,这是 PowerShell 的已知错误/限制.一个合适且实际上不错的解决方法是使用 Move-Item
来重命名项目:它具有 -LiteralPath
参数,该参数在 Rename-Item
中缺失>.
Unfortunately this is a known bug/limitation of PowerShell. A suitable and actually not bad workaround is to use Move-Item
for renaming items: it has the -LiteralPath
parameter which is missing in Rename-Item
.
查看报告的问题:
https://connect.microsoft.com/PowerShell/反馈/详细信息/553052/rename-item-literalpath
这篇关于使用 Powershell 替换方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文