Powershell: Delete images of certain dimensions

百般思念 提交于 2021-01-29 17:53:21

问题


I want to delete all *.jpg files in a specific folder and all its subfolders which have e.g. width unequal 800 and height unequal 600 (leaving only 800x600 jpg images).

Can someone tell me how to do this in Powershell?

I know I can remove *.jpg images with

Get-ChildItem -Path .\ -Filter *.jpg -Recurse | foreach ($_) {remove-item $_.fullname}

But I can't seem to find how to select height/width of an image.


回答1:


You could use the System.Drawing.Image .NET Object:

$(Get-ChildItem -Filter *.jpg).FullName | ForEach-Object { 
    $img = [Drawing.Image]::FromFile($_); 
    $dimensions = "$($img.Width) x $($img.Height)"

    If ($dimensions -eq "800 x 600") {
        Remove-Item $_
    }
}


来源:https://stackoverflow.com/questions/26314980/powershell-delete-images-of-certain-dimensions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!