【问题标题】:PowerShell with Square Brackets带方括号的 PowerShell
【发布时间】:2016-03-18 06:03:33
【问题描述】:

所以,我已经阅读了很多关于使用 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

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

【问题讨论】:

  • 你试过rename-item cmdlet吗?
  • @jisaak rename-item cmdlet 由于括号和缺少-LiteralPath 而失败。那是最初需要切换到-Move-Item。 [] 是 powershell 中的通配符

标签: powershell


【解决方案1】:

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

【讨论】:

  • 这就是解决方案。我厌倦了使用ForEach-Object,但我也错过了一些东西。我感谢帮助和额外的指导。这是一个经过彻底测试的答案,在我的情况下使用它没有任何问题。
【解决方案2】:

为了绕过括号,用双反引号`` 转义它,就像这样,

gci c:\temp\brackets *.crypted | % { 
  move-item $_.FullName.replace('[','``[') $_.FullName.Replace(".crypted", "")
}

需要将反引号加倍,以便通过反引号而不将其解释为括号的转义字符。这有点混乱,所以它看起来像是 Powershell 中的一个错误或非常令人惊讶的行为。

【讨论】:

  • 如果使用-LiteralPath,则不需要使用反引号,可以避免编写乱码代码。我还说明了为什么 OPs 原始命令会出错。
  • @vonPryz 这个解决方案也有效。也感谢您的帮助。虽然-LiteralPath 选项是一种更简洁的方式,但我也可以看到将来使用它并非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2015-04-26
  • 2021-01-11
  • 1970-01-01
  • 2012-10-09
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 2011-05-04
相关资源
最近更新 更多