调试提示:当您遇到代码问题并且正在使用管道时,请重写代码以不使用管道并将问题分解为多个步骤并插入调试辅助工具以帮助进行故障排除。可以是 Write-Host,保存到临时变量等。
对我来说,您所写的 Move-Item 不起作用,并且我收到了类似的错误消息。
这是我的解决方案:
Get-ChildItem *.crypted | ForEach-Object {Move-Item -LiteralPath $_.FullName $_.FullName.Replace('.crypted', '')}
请注意,我在 -LiteralPath 之后将 2 个参数传递给 Move-Item,并且不需要反引号或任何不寻常的东西。
这是我展示问题的工作以及我的解决方案。
D:\test\move> dir
Directory: D:\test\move
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/17/2016 9:21 PM 100347 file [1].pdf
D:\test\move> Get-ChildItem *.pdf | Move-Item -LiteralPath {$_.FullName.Replace('1', '2')}
Move-Item : Cannot move item because the item at 'D:\test\move\file [2].pdf' does not exist.
At line:1 char:23
+ ... ldItem *.pdf | Move-Item -LiteralPath {$_.FullName.Replace('1', '2')}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand
D:\test\move> Get-ChildItem *.pdf | ForEach-Object {Move-Item -LiteralPath $_.FullName $_.FullName.Replace('1', '2')}
D:\test\move> dir
Directory: D:\test\move
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/17/2016 9:21 PM 100347 file [2].pdf
也适用于扩展...
D:\test\move> dir
Directory: D:\test\move
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/17/2016 9:21 PM 100347 file [2].txt.crypted
D:\test\move> Get-ChildItem *.crypted | ForEach-Object {Move-Item -LiteralPath $_.FullName $_.FullName.Replace('.crypted', '')}
D:\test\move> dir
Directory: D:\test\move
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/17/2016 9:21 PM 100347 file [2].txt
D:\test\move>