使用PowerShell根据维度删除图片

时间:2016-05-23 14:57:57

标签: image powershell metadata delete-file

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

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

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

非常感谢任何帮助!!

这是我的代码:

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+)','$1' -gt ($w) -replace '\D+(\d+)','$1') {

        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"

1 个答案:

答案 0 :(得分:1)

试试这个

$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