Windows Powershell script to sort photograph files into Date taken-based folders as per yyyy/MM/yyyy_MM_dd

PS C:\Users\User> g:
PS G:\> cd G:\Raw_Photographs
PS G:\Raw_Photographs> $sourceDir = "G:\Raw_Photographs"
$files = Get-ChildItem -Path $sourceDir -Attributes !Directory
foreach ($file in $files) {
    $dateTaken = &"C:\Users\User\Downloads\exiftool-12.56\exiftool.exe" -DateTimeOriginal -S -s $file.FullName
    # Check if the Date taken metadata is not empty
    if (![string]::IsNullOrWhiteSpace($dateTaken)) {
        $dateTime = [DateTime]::ParseExact($dateTaken, "yyyy:MM:dd HH:mm:ss", $null)        
	$directoryPath = "$sourceDir\$($dateTime.ToString('yyyy\\MM\\yyyy_MM_dd'))"
        if (!(Test-Path $directoryPath)) {
            New-Item -ItemType Directory -Path $directoryPath | Out-Null      
        }
        $newFilePath = "$directoryPath\$($file.Name)"
        Move-Item -Path $file.FullName -Destination $newFilePath      
    } 
}

Dated ~ February 2023


Related Items