Last active
May 13, 2016 08:46
-
-
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
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
# 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script as it is will only work from ISE, see
http://stackoverflow.com/a/6040725/874701