gpt4 book ai didi

带方括号的 PowerShell

转载 作者:行者123 更新时间:2023-12-02 22:36:58 25 4
gpt4 key购买 nike

因此,我阅读了很多有关使用 Powershell 重命名文件的方括号 [] 问题。这些帖子中的大多数都谈到了删除括号。我需要保留括号,只删除文件扩展名 .crypted(CryptoLocker)。有 400,000 多个文件和 172,000 多个文件夹。我尝试了 Move-Item cmdlet...

Get-ChildItem c:\temp *.crypted | Move-Item -LiteralPath {$_.FullName.Replace(".crypted", "")} 

我得到一个错误 Move-Item : Cannot move item because the item at 'C:\temp\Rule [1].txt' does not exist

您可能会看到新路径是正确的,但它说它不存在。我很难过。任何帮助将不胜感激。

最佳答案

调试提示:当您遇到代码问题并且正在使用管道时,请重写代码以不使用管道并将问题分解为多个步骤并插入调试辅助工具以帮助解决问题。可以是 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>

关于带方括号的 PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36077232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com