Skip to content

Instantly share code, notes, and snippets.

@yipo
Last active April 11, 2021 07:03
Show Gist options
  • Save yipo/6c2bf8c7c9e6e5be6871082496221765 to your computer and use it in GitHub Desktop.
Save yipo/6c2bf8c7c9e6e5be6871082496221765 to your computer and use it in GitHub Desktop.
Get taken datetime from images.
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