Created
July 20, 2023 08:35
-
-
Save lcloss/ac544014c2f0bbed94d154fe56cf17d8 to your computer and use it in GitHub Desktop.
Windows Shell script to organize photos in subfolders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Description: Organize photos by date taken | |
# | |
# Example: | |
# | |
# From path where photos are, type: | |
# | |
# OrgPhotos | |
# | |
# > It will create subfolders grouped by Year and then by date, moving all | |
# photos to their respective folders. | |
# | |
function OrgPhotos | |
{ | |
param | |
( | |
$recursive = 'false' | |
) | |
# https://stackoverflow.com/questions/6834259/how-can-i-get-programmatic-access-to-the-date-taken-field-of-an-image-or-video | |
$sysDrawing = [reflection.assembly]::LoadWithPartialName("System.Drawing") | |
$StartFolder = Get-Location | |
Write-host $recursive | |
if ( $recursive -eq 'true' ) { | |
$Files = Get-ChildItem -Recurse -Path $StartFolder -filter *.jpg | |
Write-Host "Defined recursive" | |
} else { | |
$Files = Get-ChildItem -Path $StartFolder -filter *.jpg | |
Write-Host "Defined non recursive" | |
} | |
foreach ($file in $Files) { | |
$pic = New-Object System.Drawing.Bitmap($file.fullname) | |
try { | |
$bitearr = $pic.GetPropertyItem(36867).Value | |
$stringDate = [System.Text.Encoding]::ASCII.GetString($bitearr) | |
$pic.Dispose() | |
} catch { | |
Write-Host "No date information: $($file.name)" -fore Red | |
$stringDate = "" | |
} | |
If ($stringDate -ne "") { | |
$yearTaken = $stringDate.Substring(0,4) | |
$monthTaken = $stringDate.Substring(5,2) | |
$dayTaken = $stringDate.Substring(8,2) | |
$dateTaken = $yearTaken + "_" + $monthTaken + "_" + $dayTaken | |
$yearPath = ".\" + $yearTaken | |
If (-not (Test-Path -Path $yearPath)) { | |
$yearFolder = New-Item -ItemType Directory $yearPath | |
} | |
$imgPath = $yearPath + "\" + $dateTaken | |
If (-not (Test-Path -Path $imgPath)) { | |
$dayFolder = New-Item -ItemType Directory $imgPath | |
} | |
$comparePath = Resolve-Path $imgPath | |
If ( $file.fullname -ne "$($comparePath)\$($file.name)" ) { | |
If ( -not (Test-Path -Path "$($comparePath)\$($file.name)" -PathType Leaf )) { | |
Write-Host "Move $($file.name) to $($imgPath)" -fore Green | |
# Move-Item -Path $file.fullname -Destination $imgPath -Force | |
Move-Item -Path $file.fullname -Destination $imgPath | |
} else { | |
Write-Host "Ficheiro já existe: $($imgPath)\$($file.name)" -Fore Red | |
} | |
} | |
} | |
} | |
$Files = $null | |
} | |
OrgPhotos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment