Skip to content

Instantly share code, notes, and snippets.

@ha1t
Created April 28, 2021 07:12
Show Gist options
  • Save ha1t/eff9a9976f7d65f7ae0baa28469b763a to your computer and use it in GitHub Desktop.
Save ha1t/eff9a9976f7d65f7ae0baa28469b763a to your computer and use it in GitHub Desktop.
画像のファイル名にEXIFから取得できる撮影日時を付与する
#
# 以下のURLを参考に適当なショートカットを作成して、リンク先を powershell -NoProfile -File ファイル名(.ps1) とし
# http://misohena.jp/blog/2017-10-16-drag-and-drop-to-powershell-script.html
# 画像ファイルをドロップするとファイル名の手前に撮影日時をつけてくれる
# 例外処理はいれてないのでバックアップとった上で動かす必要がある
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
# Write-Output hoge
# [System.Windows.Forms.MessageBox]::Show("hoge")
function read_exif($filepath)
{
$img = New-Object Drawing.Bitmap($filepath)
# オリジナル画像データの生成日時を取得
$byteAry = ($img.PropertyItems | Where-Object{$_.Id -eq 36867}).Value
$img.Dispose()
# 取得した日時を表示
$strBaseDate = [System.Text.Encoding]::UTF8.GetString($byteAry) # バイト配列を文字列に変換
$strBaseDate = $strBaseDate -replace ":", "-" -replace " ", "_"
return $strBaseDate.Substring(0, 19) #末尾に終端をしめすぽい何かがついてくるのでTrim
}
foreach ($arg in $Args) {
$exif = read_exif($arg)
$baseName = [System.IO.Path]::GetFileName($arg)
$afterName = $exif + "_" + $baseName
$newName = $arg -replace $baseName, $afterName
Rename-Item -Path $arg -NewName $newName
#[System.Windows.Forms.MessageBox]::Show($afterName)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment