【发布时间】:2020-12-21 16:15:33
【问题描述】:
我问了一个问题,我尝试使用 PowerShell 从众多文件夹中的众多文件的名称中删除部分内容。可能需要删除的部分是“-xx_xx”“-yy_yy”“-zz_zz”等。
为此,我正在使用这个:
Get-Childitem -Recurse | Rename-Item -NewName { $_.Name -replace " - xx_xx","" -replace " - yy_yy","" -replace " - zz_zz",""}
效果很好。
但是,我收到一条说明,指出对于特定项目,命名约定将会改变。类似于“-xx_xx-yy_yy-A-B”或“-xx_xx_zz_zz-A-B”或“-xx_xx_yz_yz-A-B”等...注意没有空格。我以为我可以简单地将其放入其中,它会起作用,例如:
Get-Childitem -Recurse | Rename-Item -NewName { $.Name -replace "-xx_xx-yy_yy-A-B","" -replace "-xx_xx_zz_zz-A-B","" -replace "-xx_xx_yz_yz-A-B","" }
但是......它不起作用。我认为这可能与连字符有关,但除非我以某种方式错误地这样做,否则将它们转义也不起作用,这是很有可能的。
这是我收到的错误消息:
Rename-Item : The input to the script block for parameter 'NewName' failed. The term '$.Name' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:47
+ ... em -NewName { $.Name -replace "-xx_xx-yy_yy-A-B","" -replace "-xx_xx- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (True Dialog Requests-xx_xx-yy_yy-A-B.htm:PSObject) [Rename-Item], Para
meterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentInvocationFailed,Microsoft.PowerShell.Commands.RenameItemCommand
- 如何让它发挥作用
- 为什么它可以与“-xx_xx”等一起使用,而不是现在?
- 是否有更好的方法可以同时兼顾两者?
【问题讨论】:
-
最好先完成get-childitem。
(Get-Childitem -Recurse) | rename-item
标签: powershell rename