HDI Powershell: Se detectar linha no arquivo, exclua o arquivo associado

1

Estou tentando remover uma enorme quantidade de jpegs corrompidos de uma biblioteca de imagens. Usando o jpegsnoop.exe, criei um arquivo [$ jpgname.txt] para cada imagem. Os jpegs corruptos terão "ERRO" em algum lugar no arquivo jpgname.txt.

Até agora, posso detectar todos os arquivos .txt que sinalizam arquivos inválidos com os abaixo:

gci ./ "* .txt" | Select-String -pattern "ERROR" | Caminho da Format-Table -GroupBy

Produz algo assim para cada arquivo detectado (existem milhares):

Path: H:\library
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
Path: H:\library
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%pre%10712x1024.jpg.txt: **The process cannot acces
s the file 'H:\library%pre%10712x1024.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%pre%10_54165.jpg.txt: The process cannot access
the file 'H:\library%pre%10_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.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}
10712x1024.jpg.txt: **The process cannot acces s the file 'H:\library%pre%10712x1024.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%pre%10_54165.jpg.txt: The process cannot access the file 'H:\library%pre%10_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.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}

A pergunta é: Como eu vou daqui para deletar o arquivo retornado na linha "Path" E é equivalente a jpeg? Ou seja, excluindo H: \ library \ 001.AE3923.jpg.txt e H: \ library \ 001.AE3923.jpg para cada arquivo retornado pelo gci / grep.

Obrigado.

Resposta ao EBGreen:

Obrigado por responder: está muito mais perto. Eu recebo os seguintes erros, no entanto:

%pre%     
por User 92873492834 03.08.2012 / 23:20

1 resposta

0

tente isso:

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}
    
por 03.08.2012 / 23:25

Tags