使用 PowerShell 根据尺寸删除图片

Deleting pictures based on dimensions with PowerShell

我想删除某个文件夹中的所有 VERTICAL 和 SMALL 图片。

我有一个文件夹,我不想要垂直图片或尺寸小于 600 x 600 像素的图片。我相信 PowerShell 是最好用的东西,因为我无法让 Python 在我的计算机上工作。

我这样做是因为我不想每天从我的文件夹中手动删除 vertical/small 图片。 (每天都有新的)

任何帮助将不胜感激!!

这是我的代码:

cd C:\Users\Jack\Desktop\Test

$c = 5
Function Get-FileMetaData {

    Param([string[]]$folder)
    foreach($sFolder in $folder) {
        $a = 0
        $b = 1

        $objShell = New-Object -ComObject Shell.Application
        $objFolder = $objShell.namespace($sFolder)

        foreach ($File in $objFolder.items()) {

            $FileMetaData = New-Object PSOBJECT
            for ($a ; $a -le 266; $a++) {
                if($objFolder.getDetailsOf($File, $a)) {

                    $hash += @{$($objFolder.getDetailsOf($objFolder.items, $a)) =
                    $($objFolder.getDetailsOf($File, $a)) }
                    $FileMetaData | Add-Member $hash

                    if ($($objFolder.getDetailsOf($objFolder.items, $a)) -eq "Height") {
                        Write-Host $($objFolder.getDetailsOf($objFolder.items, $a)) =====
                        $($objFolder.getDetailsOf($File, $a))
                    }

                    if ($($objFolder.getDetailsOf($objFolder.items, $a)) -eq "Width") {
                        Write-Host $($objFolder.getDetailsOf($objFolder.items, $a)) =====
                        $($objFolder.getDetailsOf($File, $a))
                    }

                    if ($($objFolder.getDetailsOf($objFolder.items, $a)) -eq "Name") {
                        Write-Host $($objFolder.getDetailsOf($objFolder.items, $a)) =====
                        $($objFolder.getDetailsOf($File, $a))
                    }

                    $b++
                    $hash.clear()

                } #end if
            } #end for
        Write-Host $a
        $a=0
        $FileMetaData
        } #end foreach $file
    $c++
    Write-Host c = $c
    } #end foreach $sfolder
} #end Get-FileMetaData

Write-Host c = $c

$h = Get-FileMetaData C:\Users\Jack\Desktop\Test | select Height
$w = Get-FileMetaData C:\Users\Jack\Desktop\Test | select Width
$n = Get-FileMetaData C:\Users\Jack\Desktop\Test | select Name

$h
Write-Host w = $w
Write-Host name = $n

$SpecChars = '!', "{", "}", '"', '£', '$', '%', '&', '^', '*', '(', ')', '@', '=', '+', '¬', '`', '\', '<', '>', '?', '/', ':', ';', '#', '~', "'", '-', "Name", "N", "a", "m", "e", ' '
$remspecchars = [string]::join('|', ($SpecChars | % {[regex]::escape($_)}))


if (($h) -replace '\D+(\d+)','' -gt ($w) -replace '\D+(\d+)','') {

        Write-Host "VERTICAL"

        Write-Host name = $n

        $d = $n -replace $remspecchars, ""
        $d.split()
        Write-Host $d
        $tally = 0
        while($tally -ne $d.Count) {
            del $d[$tally]
            $tally++
        }


        $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }

Write-Host "Finished"

试试这个

$folder = 'C:\temp\Pictures\'

$image = New-Object -ComObject Wia.ImageFile

$pictures = Get-ChildItem $folder *.jpg | ForEach-Object {
    $image.LoadFile($_.fullname)
    $size = $image.Width.ToString() + 'x' + $image.Height.ToString()

    $orientation = $image.Properties | ? {$_.name -eq 'Orientation'} | % {$_.value}
    if ($orientation -eq 6) {
        $rotated = $true
    } else {
        $rotated = $false
    }

    $heightGtWidth = if ([int]$image.Height.ToString() -gt [int]$image.Width.ToString()) {
        $true
    } else {
        $false
    }

    [pscustomobject]@{
        Fullname = $_.FullName
        Size = $size
        Rotated = $rotated
        HeightGtWidth = $heightGtWidth
    }
}

$pictures