Jacksmackod
Jacksmackod

Reputation: 87

Deleting pictures based on dimensions with PowerShell

I would like to delete all VERTICAL and SMALL pictures from a certain folder.

I have a folder that I do not want the vertical pictures or pictures with dimensions less than 600 x 600 pixels. I believe that PowerShell is the best thing to use since I cannot get Python to work on my computer.

I am doing this because I do not want to manually delete the vertical/small pictures from my folder each day. (It gets new ones every day)

Any help would be greatly appreciated!!

Here is my code :

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"

Upvotes: 0

Views: 920

Answers (1)

Anthony Stringer
Anthony Stringer

Reputation: 2001

try this

$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

Upvotes: 1

Related Questions