Last active
April 11, 2021 07:03
-
-
Save yipo/6c2bf8c7c9e6e5be6871082496221765 to your computer and use it in GitHub Desktop.
Get taken datetime from images.
This file contains 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
Add-Type -AssemblyName 'System.Drawing' | |
function Get-ExifDate ([String]$File) { | |
try { | |
$bitmap = New-Object System.Drawing.Bitmap -ArgumentList $File | |
$binary = $bitmap.GetPropertyItem(0x9003).Value | |
$string = [System.Text.Encoding]::ASCII.GetString($binary, 0, $binary.Length - 1) | |
return [DateTime]::ParseExact($string, 'yyyy:MM:dd HH:mm:ss', $null) | |
} | |
catch { | |
return $null | |
} | |
finally { | |
if ($bitmap) { | |
$bitmap.Dispose() | |
} | |
} | |
} | |
function Rename-ByExifDate ([String]$Directory) { | |
Get-ChildItem $Directory -Recurse -File | ForEach-Object { | |
$date = Get-ExifDate $_.FullName | |
if (-not $date) { | |
Write-Host $_.FullName '-- ignored (no information)' | |
return | |
} | |
$name = Get-Date $date -Format 'yyyy-MM-dd_HH.mm.ss' | |
if ($name -eq $_.BaseName) { | |
Write-Host $_.FullName '-- ignored (already is)' | |
return | |
} | |
$ext = $_.Extension.ToLower() | |
Write-Host $_.FullName ---> $name$ext -ForegroundColor Cyan | |
Rename-Item $_.FullName -NewName "$name$ext" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment