Skip to content

Instantly share code, notes, and snippets.

@russellds
Created July 28, 2014 02:39
Show Gist options
  • Save russellds/27e67ec548e1c2b37e4c to your computer and use it in GitHub Desktop.
Save russellds/27e67ec548e1c2b37e4c to your computer and use it in GitHub Desktop.
Rename pictures to date taken using Powershell
function Rename-PictureToDateTaken {
param(
[string]$Path
)
$assemblyLoaded = [appdomain]::currentdomain.getassemblies() |
where { $_ -match 'System.Drawing' }
if( -not $assemblyLoaded ) {
Add-Type -AssemblyName System.Drawing
}
$files = Get-ChildItem -Path $Path
foreach( $file in $files ) {
$fs = New-Object System.IO.FileStream $file.FullName, 'Open', 'Read'
$image = [Drawing.Image]::FromStream($fs, $false, $false)
$imageProperty = $image.GetPropertyItem(36867)
$fs.Close()
$dateTaken = [Text.Encoding]::UTF8.GetString($imageProperty.Value) #-replace ":", "-" -replace " ", "_"
$dateTaken = $dateTaken.Substring(0, ($dateTaken.Length - 1)) -replace ":", "-" -replace " ", "_"
$newName = $dateTaken + $file.Extension
#Start-Sleep -Milliseconds 100
Rename-Item -Path $file.FullName -NewName $newName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment