[英]Rename-Item is not replacing the file name
in following PowerShell code:在以下 PowerShell 代码中:
dir "D:\Test\New folder" | Rename-Item -NewName {$_.Name -replace 'PP&C Test (Bank), Ltd.','PP&C Test (Bank) Ltd.'}
what I am trying to do is replace all files within D:\Test\New folder that have PP&C Test (Bank), Ltd. in their filename with PP&C Test (Bank) Ltd. So basically just to remove the comma before Ltd.我要做的是用 PP&C Test (Bank) Ltd. 替换 D:\Test\New 文件夹中文件名中包含 PP&C Test (Bank), Ltd. 的所有文件。所以基本上只是删除 Ltd. 之前的逗号。
When executing the script in PowerShell nothing gets renamed.在 PowerShell 中执行脚本时,没有重命名。 Any idea why?知道为什么吗? I believe it is something to do with & and Brackets () as when I'm doing the same for filenames without those chars it works fine.我相信这与 & 和 Brackets () 有关,因为当我对没有这些字符的文件名执行相同操作时,它可以正常工作。
Thanks!谢谢!
Or backslash the parens for the regex.或反斜杠正则表达式的括号。 I wonder if -replace should give a warning.我想知道 -replace 是否应该发出警告。 The parens are used for grouping.括号用于分组。
'PP&C Test (Bank), Ltd.' -replace 'PP&C Test \(Bank\), Ltd.',
'PP&C Test (Bank) Ltd.'
PP&C Test (Bank) Ltd.
What escape() returns. escape() 返回什么。 I don't see why the spaces are escaped.我不明白为什么空格会被转义。 .
means any character.表示任何字符。
[regex]::escape('PP&C Test (Bank), Ltd.')
PP&C\ Test\ \(Bank\),\ Ltd\.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.