Skip to content

Instantly share code, notes, and snippets.

@thorade
Last active May 13, 2016 08:46
Show Gist options
  • Save thorade/5970020 to your computer and use it in GitHub Desktop.
Save thorade/5970020 to your computer and use it in GitHub Desktop.
using Powershell and IrfanView to rotate and rename all jpg files in the directory of the script
# Declare functions first
# this function reads the "date taken" from extended properties
function Get-DateTaken {
Param(
[string]$filePath
)
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $filePath
$file = Split-Path $filePath -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)
$takenDateID = 12
$takenDate = $shellfolder.GetDetailsOf($shellfile, $takenDateID)
$asci = [System.Text.Encoding]::ASCII
$asciDate = $asci.GetString($asci.GetBytes($takenDate))
$date= $asciDate -replace '\?'
$date = [DateTime]::Parse($date);
return $date
}
# get current directy
$curDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# Autorotate and optimize using IrfanView
# Set File CreationDate to Exif Date
# Warning: this may change the Hash of the file
$argList = "$curDir\*.jpg /jpg_rotate=(6,1,1,0,1,300,0,0)"
Start-Process -Wait -FilePath "${env:ProgramFiles}\IrfanView\i_view32.exe" -ArgumentList $argList
# get a list of jpg files
$jpgFiles = Get-ChildItem $curDir\*.jpg | sort-object -property CreationTime
# Rename jpg files to Date_Time_Hash.jpg
ForEach ($pic In $jpgFiles){
$HashString = ((Get-FileHash $pic -Algorithm SHA256).Hash).Remove(4)
$NeuerName = (Get-DateTaken($pic)).toString("yyyyMMdd_HHmmss") + "_" + $HashString + ".jpg"
Rename-Item $pic -NewName $NeuerName
}
@thorade
Copy link
Author

thorade commented May 9, 2015

Use Hash instead of random, to get reproducible results:
http://stackoverflow.com/q/26930090/874701

Get-FileHash -Algorithm SHA256
If no value is specified, or if the parameter is omitted, the default value is SHA256
https://technet.microsoft.com/de-de/library/dn520872.aspx

If IrfanView has not been run, the CreationDate is wrong.
In this case, the function Get-DateTaken is more reliable.
Inspired by: http://www.zold.org/zold/ps_archive_images_takendate/
http://stackoverflow.com/q/9552367/874701

@thorade
Copy link
Author

thorade commented May 9, 2015

The script as it is will only work from ISE, see
http://stackoverflow.com/a/6040725/874701

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment