1

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
3
  • So a couple of those errors indicate that the files are locked by another process. The first one indicates that there is some problem with the path\filename that is being built. Since the file lock errors are presumably pointing to valid file names, I would say that the code is right or close to it.
    – EBGreen
    Aug 3, 2012 at 21:53
  • I'm not sure, but since I closed everything else that's talking to that directory and still get the same results, I think powershell itself might be locking the files (with the previous commands). Have you heard of that? Thanks. Aug 3, 2012 at 22:47
  • I have not heard of that, but often the ways of the Windows is a mystery to us all...
    – EBGreen
    Aug 4, 2012 at 0:05

1 Answer 1

0

try this:

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}
2
  • Correction: The answer you provided is deleting corrupted files the way it's supposed to. I don't know why it's throwing errors (so i terminated it early the first few times), but on closer inspection, it's doing what it's supposed to. Aug 3, 2012 at 22:52
  • Can't explain the errors, but it definitely works. Thanks for your help! Aug 3, 2012 at 22:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .