I'm trying to remove a huge amount of corrupt jpegs from an image library. Using jpegsnoop.exe, I created a [$jpgname.txt] file for every picture. The corrupt jpegs will have "ERROR" somewhere in the jpgname.txt file.
So far, I can detect all of the .txt files that flag bad files with the below:
gci ./ "*.txt" | Select-String -pattern "ERROR" | Format-Table -GroupBy Path
It outputs something like this for every file it detects (there are thousands):
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}
The question is: How do I go from here to deleting the file returned in the "Path" line AND it's jpeg equivalent? That is, deleting both H:\library\001.AE3923.jpg.txt and H:\library\001.AE3923.jpg for every file returned by the gci / grep.
Thank you.
Response to EBGreen:
Thanks for responding: It's much closer. I get the following errors, though:
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