我在文件夹中有一些图像
van-map-PE 100-1.png
van-map-PE 200-8.png
van-map-PE 160-2.png
van-map-PE 400-6.png
现在我想删除之间的所有空格PE xxx-
并将其替换-
为
van-map-PE-100-1.png
van-map-PE-200-8.png
van-map-PE-160-2.png
van-map-PE-400-6.png
我在 Power shell 中使用了这个命令
PS D:\imgs\png> get-childitem *.png | foreach { rename-item $_ $_.name.replace( "PE ","PE-")}
但我收到了这个错误
ForEach-Object : Cannot bind parameter 'RemainingScripts'. Cannot convert the "" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
At line:1 char:78
+ ... item *.png | foreach { rename-item $_ $_.name.replace( "PE ","PE-")}
+ ~
+ CategoryInfo : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand
答案1
您的语法是正确的。问题在于语句末尾隐藏了一个不需要的字符。请注意~
错误中的 如何在代码后面划出看似空白的部分。您有一个不需要的字节顺序标记 (BOM)在最后一个花括号之后。在十六进制编辑器中查看代码会发现以下内容:
解决
从代码末尾开始,按退格键直到右花括号消失。这将确保您已删除 BOM。删除右花括号后,再次添加它。或者,复制此语句的修订版本:
gci *.png | % { ren $_ $_.name.replace(" ","-")}
附加信息
EF BB BF
( 
) 是 UTF-8 的 BOM。此问题仅出现在您的原始帖子中。由于您的帖子已被编辑,因此我没有在您的 Powershell 代码中看到 BOM 字符(尽管我没有看到它已通过编辑明确删除)。如果您查看原始帖子的历史记录,您将能够捕获不需要的 BOM。
答案2
以下是一个可以帮您完成此任务的方法。当然,其他人也会有自己的看法。
'van-map-PE 100-1.png',
'van-map-PE 200-8.png',
'van-map-PE 160-2.png',
'van-map-PE 400-6.png' |
%{New-Item -Path 'D:\Temp\PngFiles' -Name "$_" -ItemType File}
Get-ChildItem -Path 'D:\Temp\PngFiles'
Directory: D:\Temp\PngFiles
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/28/2018 8:59 PM 0 van-map-PE 100-1.png
-a---- 9/28/2018 8:59 PM 0 van-map-PE 160-2.png
-a---- 9/28/2018 8:59 PM 0 van-map-PE 200-8.png
-a---- 9/28/2018 8:59 PM 0 van-map-PE 400-6.png
Get-ChildItem -Path 'D:\Temp\PngFiles' |
ForEach {
$NewFileName = $_.FullName -replace ' ','-'
Rename-Item -Path $_.FullName -NewName $NewFileName -Verbose -WhatIf
}
Get-ChildItem -Path 'D:\Temp\PngFiles' |
ForEach {
$NewFileName = $_.FullName -replace ' ','-'
Rename-Item -Path $_.FullName -NewName $NewFileName -Verbose -WhatIf
}
What if: Performing the operation "Rename File" on target "Item: D:\Temp\PngFiles\van-map-PE 100-1.png Destination: D:\Temp\PngFiles\van-map-PE-100-1.png".
What if: Performing the operation "Rename File" on target "Item: D:\Temp\PngFiles\van-map-PE 160-2.png Destination: D:\Temp\PngFiles\van-map-PE-160-2.png".
What if: Performing the operation "Rename File" on target "Item: D:\Temp\PngFiles\van-map-PE 200-8.png Destination: D:\Temp\PngFiles\van-map-PE-200-8.png".
What if: Performing the operation "Rename File" on target "Item: D:\Temp\PngFiles\van-map-PE 400-6.png Destination: D:\Temp\PngFiles\van-map-PE-400-6.png".
Get-ChildItem -Path 'D:\Temp\PngFiles' |
ForEach {
$NewFileName = $_.FullName -replace ' ','-'
Rename-Item -Path $_.FullName -NewName $NewFileName -Verbose # -WhatIf
}
Get-ChildItem -Path 'D:\Temp\PngFiles'
Get-ChildItem -Path 'D:\Temp\PngFiles'
Directory: D:\Temp\PngFiles
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/28/2018 8:59 PM 0 van-map-PE-100-1.png
-a---- 9/28/2018 8:59 PM 0 van-map-PE-160-2.png
-a---- 9/28/2018 8:59 PM 0 van-map-PE-200-8.png
-a---- 9/28/2018 8:59 PM 0 van-map-PE-400-6.png