HDI Powershell:如果检测到文件中的行,则删除相关文件

HDI Powershell:如果检测到文件中的行,则删除相关文件

我正在尝试从图片库中删除大量损坏的 jpeg。使用 jpegsnoop.exe,我为每张图片创建了一个 [$jpgname.txt] 文件。损坏的 jpeg 在 jpgname.txt 文件的某处会出现“ERROR”。

到目前为止,我可以检测到所有标记坏文件的 .txt 文件,如下所示:

gci./“*.txt”| 选择字符串-模式“错误”| 格式表-GroupBy 路径

它对检测到的每个文件(有数千个)输出类似这样的内容:

Path: H:\library\001.AE3923.jpg.txt

IgnoreCase     LineNumber Line           Filename       Path           Pattern        Context        Matches
----------     ---------- ----           --------       ----           -------        -------        -------
     True            285     ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            286 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            287 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            288 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            290 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            291 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            292 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            294 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            295 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            296 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            298 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            299 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            301 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            302 *** ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            304   ERROR: Ex... 001.AE3923.... H:\library... ERROR                         {ERROR}
     True            307     ERROR: ... 001.AE3923.... H:\library... ERROR                         {ERROR}

问题是:我该如何从这里删除“路径”行中返回的文件及其 jpeg 等效文件?也就是说,对于 gci / grep 返回的每个文件,删除 H:\library\001.AE3923.jpg.txt 和 H:\library\001.AE3923.jpg。

谢谢。

回复 EBGreen:

感谢您的回复:它更接近了。但我收到以下错误:

Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:171
+ gci ./ "*.txt" | Select-String -pattern "ERROR" | %{$txtFile = Get-Item $_.Path; $jpgFile = Get-Item ('{0}\{1}' -f $t
xtFile.DirectoryName, $txtFile.BaseName); Remove-Item <<<<  $jpgFile; Remove-Item $txtFile}
    + CategoryInfo          : InvalidData: (:) [Remove-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveItemC
   ommand

Remove-Item : Cannot remove item H:\library\0010712x1024.jpg.txt: **The process cannot acces
s the file 'H:\library\0010712x1024.jpg.txt' because it is being used by another process.**
At line:1 char:193
+ gci ./ "*.txt" | Select-String -pattern "ERROR" | %{$txtFile = Get-Item $_.Path; $jpgFile = Get-Item ('{0}\{1}' -f $t
xtFile.DirectoryName, $txtFile.BaseName); Remove-Item $jpgFile; Remove-Item <<<<  $txtFile}
    + CategoryInfo          : WriteError: (H:\library\__I...12x1024.jpg.txt:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item H:\library\0010_54165.jpg.txt: The process cannot access
the file 'H:\library\0010_54165.jpg.txt' because it is being used by another process.
At line:1 char:193
+ gci ./ "*.txt" | Select-String -pattern "ERROR" | %{$txtFile = Get-Item $_.Path; $jpgFile = Get-Item ('{0}\{1}' -f $t
xtFile.DirectoryName, $txtFile.BaseName); Remove-Item $jpgFile; Remove-Item <<<<  $txtFile}
    + CategoryInfo          : WriteError: (H:\library\__I...0_54165.jpg.txt:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

答案1

尝试这个:

gci ./ "*.txt" | Select-String -pattern "ERROR" | %{$txtFile = Get-Item $_.Path; $jpgFile = Get-Item ('{0}\{1}' -f $txtFile.DirectoryName, $txtFile.BaseName); Remove-Item $jpgFile; Remove-Item $txtFile}

相关内容