Skip to content

Instantly share code, notes, and snippets.

@erikvullings
Created September 4, 2023 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikvullings/6b8ce2a35c0a099faf67d2cdf49c8779 to your computer and use it in GitHub Desktop.
Save erikvullings/6b8ce2a35c0a099faf67d2cdf49c8779 to your computer and use it in GitHub Desktop.
Sort photos by EXIF date taken in a folder structure (WINDOWS).

Introduction

My camera photos on my phone all end up in one big folder. I wanted to sort them by date taken in a simple format year-month format, YYYY-MM. I tried the application PhotoMove, but unfortunately, this simple requirement is only available with the PRO version, so instead, I've asked ChatGPT to create a PowerShell script for me.

Usage:

  1. Download the Windows version from exif tool and unzip it into a folder. Make sure that the name is exiftool.exe (in my case, it had a weird name exiftool(-k).exe so rename it if that's the case).
  2. Save the PowerShell script you can find below in the same folder.
  3. Open a PowerShell window (WIN+R and enter powershell)
  4. Run the script ./sort.ps1
  5. Enter the folder name containing the original photo's (I did not test it with a folder that contains spaces)

The script will go through all your photo's, extract the EXIF date taken, and moves them to <SOURCE_FOLDER>YYYY-MM (you cannot set the output folder, so it assumes the source folder is used). This may take a while.

# Prompt the user for the source folder containing photos and videos
$sourceFolder = Read-Host "Enter the path to the source folder"
# Check if the source folder exists
if (-not (Test-Path -Path $sourceFolder -PathType Container)) {
Write-Host "Source folder does not exist. Please provide a valid path."
exit
}
# Define the path to the ExifTool executable (adjust as needed)
$exifToolPath = ".\exiftool.exe"
# Get a list of all files in the source folder
$files = Get-ChildItem -Path $sourceFolder
# Loop through each file and organize them by year and month based on EXIF date
foreach ($file in $files) {
# Check if the file is a photo or video (you can add more extensions if needed)
if ($file.Extension -match ".jpg|.jpeg|.png|.gif|.bmp|.mp4|.avi|.mov|.mkv|.wmv") {
# Use ExifTool to extract the date taken from the file
$exifDate = & $exifToolPath "-DateTimeOriginal" "-S" "-d" "%Y-%m" $file.FullName
if (-not [string]::IsNullOrWhiteSpace($exifDate)) {
# Remove the "DateTimeOriginal:" prefix
$exifDate = $exifDate -replace "DateTimeOriginal:\s*", ""
# Define the destination folder based on the EXIF date
$destinationFolder = Join-Path -Path $sourceFolder -ChildPath $exifDate
# Create the destination folder if it doesn't exist
if (-not (Test-Path -Path $destinationFolder)) {
New-Item -Path $destinationFolder -ItemType Directory
}
# Move the file to the destination folder
Move-Item -Path $file.FullName -Destination $destinationFolder
}
}
}
Write-Host "Organizing complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment