Created
March 31, 2019 17:09
-
-
Save HerrLevin/05da7942e3ef4bb5a337d62aa423da7c to your computer and use it in GitHub Desktop.
Powershell-Script for renaming (image)files to creation-date
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
# | |
# =================================================================== | |
# RENAME 3000 | |
# =================================================================== | |
# Purpose: Rename every file with extension $extension (Line 21) with EXIF-Date or CreationDate | |
# Append "(1)", "(2)", etc. for already-existent files | |
# Keeping old filename, if new one is the same | |
# | |
# Author: Levin Baumann | |
# =================================================================== | |
# | |
$nocomment = [reflection.assembly]::LoadWithPartialName("System.Drawing") | |
$count = 0 | |
[string] $extension = ".jpg" | |
$filecount = (Get-ChildItem ("*"+$extension) | Measure-Object).Count; | |
Write-Host "INFO:" -ForegroundColor Black -BackgroundColor Blue -NoNewline | |
Write-Host "Do you really want to rename" -ForegroundColor Blue -BackgroundColor White -NoNewline | |
Write-Host (" " + $filecount + " ") -ForegroundColor Blue -NoNewline | |
Write-Host "file(s) of type" -ForegroundColor Blue -BackgroundColor White -NoNewline | |
Write-Host (" " + $extension + " ") -ForegroundColor Blue -NoNewline | |
Write-Host "?" -ForegroundColor Blue -BackgroundColor White | |
Write-Host "Press any key to continue..." | |
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') | |
get-childitem ("*"+$extension) | foreach { | |
$count++ | |
$failed = 0 | |
$NewNameIsOldName = 0 | |
try { | |
$pic = New-Object System.Drawing.Bitmap($_.Name) | |
$bitearr = $pic.GetPropertyItem(36867).Value | |
$string = [System.Text.Encoding]::ASCII.GetString($bitearr) | |
$date = [datetime]::ParseExact($string,"yyyy:MM:dd HH:mm:ss`0",$Null) | |
[string] $newfilename = get-date $date -format yyyyMMdd_HHmmss | |
} | |
catch [Exception]{ | |
$failed = 1 | |
} | |
try { | |
$pic.Dispose() | |
} | |
catch { | |
Write-Host "ERROR:" -ForegroundColor Black -BackgroundColor red -NoNewline | |
Write-Host (" " + $_.Name + " ") -ForegroundColor red -NoNewline | |
Write-Host "seems to be no picture." -ForegroundColor red -BackgroundColor white | |
} | |
if ($failed -eq 1) { | |
Write-Host "ERROR:" -ForegroundColor Black -BackgroundColor red -NoNewline | |
Write-Host "No Exif-Data for" -ForegroundColor red -BackgroundColor white -NoNewline | |
Write-Host (" " + $_.Name) -ForegroundColor red | |
[string] $newfilename = $_.LastWriteTime.toString("yyyyMMdd_HHmmss") | |
} | |
if(Test-Path ($newfilename+$extension)){ | |
$exists = 1 | |
$n = 1 | |
[string] $appendix = $extension | |
if ($_.Name -eq ($newfilename + $appendix)) { | |
$NewNameIsOldName = 1 | |
} else { | |
while ($exists -eq 1) { | |
Write-Host "WARNING:" -ForegroundColor yellow -BackgroundColor black -NoNewline | |
Write-Host "Filename already exists:" -ForegroundColor black -BackgroundColor yellow -NoNewline | |
Write-Host (" " + $newfilename + $appendix) -ForegroundColor yellow | |
[string] $appendix = "(" + $n + ")"+$extension | |
if (Test-Path ($newfilename + $appendix)) { | |
if ($_.Name -eq ($newfilename + $appendix)) { | |
$exists = 0 | |
$NewNameIsOldName = 1 | |
} else { | |
$n++ | |
} | |
} else { | |
$exists = 0 | |
} | |
} | |
} | |
} else { | |
[string] $appendix = $extension | |
} | |
$newfilename += $appendix | |
try { | |
if ($NewNameIsOldName -eq 0) { | |
rename-item $_ $newfilename | |
Write-Host "SUCCESS:" -ForegroundColor green -BackgroundColor black -NoNewline | |
Write-Host "New name for" -ForegroundColor black -BackgroundColor green -NoNewline | |
Write-Host (" " + $_ + " ") -ForegroundColor green -NoNewline | |
Write-Host "is" -ForegroundColor black -BackgroundColor green -NoNewline | |
Write-Host (" " + $newfilename) -ForegroundColor green | |
} else { | |
Write-Host "INFO:" -ForegroundColor Black -BackgroundColor Blue -NoNewline | |
Write-Host "Keeping old filename for" -ForegroundColor Blue -BackgroundColor White -NoNewline | |
Write-Host (" " + $_) -ForegroundColor Blue | |
} | |
} | |
catch { | |
Write-Host "ERROR:" -ForegroundColor Black -BackgroundColor red -NoNewline | |
Write-Host "Could not write filename" -ForegroundColor red -BackgroundColor white -NoNewline | |
Write-Host (" " + $newfilename) -ForegroundColor red - NoNewline | |
Write-Host "Keeping old Filename" -ForegroundColor black -BackgroundColor green -NoNewline | |
Write-Host (" " + $_) -ForegroundColor green | |
} | |
Write-Host ("File #" + $count + "/"+$filecount) | |
} | |
Write-Host "Done. Press any key to exit..." | |
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. I got one for all files in a directory but it seems to not work.
Get-ChildItem *.* | %{Rename-Item $_ -NewName ('{0}{1}' -f $_.LastWriteTime.toString("yyyyMMdd-hhmmss_"), $_.Extension)}