Last active
July 3, 2023 18:49
-
-
Save timiles/f2e422d541b94b1e2ef3fee02fcc767e to your computer and use it in GitHub Desktop.
PowerShell script to prefix files by Date Taken from EXIF data
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
function Get-DateTakenFromExifData { | |
param([String] $filePath) | |
try { | |
$imgData = New-Object System.Drawing.Bitmap($filePath) | |
try { | |
[byte[]]$dateTakenExifData = $imgData.GetPropertyItem(36867).Value | |
[string]$dateString = [System.Text.Encoding]::ASCII.GetString($dateTakenExifData) | |
return [datetime]::ParseExact($dateString, "yyyy:MM:dd HH:mm:ss`0", $null) | |
} | |
finally { | |
$imgData.Dispose() | |
} | |
} | |
catch { } | |
return $null | |
} | |
function Get-DateTimeForFilePrefix { | |
param([System.IO.FileInfo] $fileInfo) | |
$date = Get-DateTakenFromExifData ($fileInfo.FullPath) | |
if ($date) { | |
return $date | |
} | |
else { | |
return $fileInfo.LastWriteTime | |
} | |
} | |
Get-ChildItem | ForEach-Object { | |
$from = $_.Name | |
$dateTimeForPrefix = Get-DateTimeForFilePrefix $_ | |
$to = "$($dateTimeForPrefix.ToString('yyyyMMdd_HHmmss')) $from" | |
Write-Host "Renaming `"$from`" to `"$to`"" | |
Rename-Item $from $to | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment