Reputation: 13
Looking for a powershell script that can simultaneously delete *.jpg files by image size (all *.jpg less than 6000 bytes), AND simultaneously read the name of that image file and use that to search for and delete the correspondingly named *labelled.png file. There are some special characters ("[" "]" "=" " ") in the file names unfortunately, but these can be removed or replaced by "_" on beforehand, or ideally simultaneously with the same script if possible.
Here is what I use now to delete the *.jpg files less than 6000 bytes:
ls -Filter *.jpg -recurse -file | where {$_.Length -lt 6000} | Remove-Item -Force -recurse
Is there some code I can add to have the script also delete the correspondingly named *labelled.png-file if the *.jpg file is less than 6000 bytes (and first or simultaneously remove special characters)?
Upvotes: 1
Views: 328
Reputation: 17055
Instead of piping directly into Remove-Item
you can use ForEach-Object and do both delete operations there:
ls -Filter *.jpg -recurse -file | where {$_.Length -lt 6000} | foreach {
Remove-Item -LiteralPath $_.FullName -Force
Remove-Item -LiteralPath ($_.FullName -replace "\.jpg", "-labelled.png") -Force
}
Upvotes: 3