user14575983
user14575983

Reputation: 13

Powershell script to delete *.jpg files smaller than 6000 bytes, and delete the correspondingly named *labelled.png file?

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)?

Screenshot from part of the list of image-files (*.jpg) and correspondingly named (*labelled.png) mask files to be deleted

Upvotes: 1

Views: 328

Answers (1)

marsze
marsze

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

Related Questions