我在使用rename-item时遇到了问题。需要将文件夹的名称更改为“01”,“02”,“03”……已经尝试了所有方法,但最终出现了“项目不存在”的错误提示。非常抱歉这个问题比较简单,但我已经寻找解决方案一整天了。
PS C:\Users\admin>
$nr = 1
Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
Rename-Item : Cannot rename because item at 'ert' does not exist.
At line:3 char:23
+ Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot rename because item at 'ukh' does not exist.
At line:3 char:23
+ Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot rename because item at 'yph' does not exist.
At line:3 char:23
+ Dir E:"Data-test" | %{Rename-Item $_ -NewName (‘{0}’ -f $nr++)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
$_.FullName
的提示,但请注意问题中的命令(只有一个)并不完美工作,因为它确实会触发文件名的字符串化。情境名称字符串化问题困扰着Windows PowerShell,但已在PowerShell [Core] v6.1+中得到解决 - 有关详细信息,请参见此答案。 - mklement0