Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - rename file using Date Taken attribute

I have a stack load of images and videos on my Samsung phone. I copied these images to a USB then onto my PC.

I want to use Powershell to rename these files based on their Date Taken attribute.

Format required = yyyy-MM-dd HH.mm.ss ddd

I have been using a Powershell script (see below) that does this beautifully using the Date Modified attribute, but the copy above somehow changed the Date Modified value on me (WTH!), so I can't use that now (as its not accurate).

Get-ChildItem | Rename-Item -NewName {$_.LastWriteTime.ToString("yyyy-MM-dd HH.mm.ss ddd") + ($_.Extension)}

In summary - is there a way to change the file name based on the Date Taken file attribute? Suggestions I have seen online require use of the .NET System.Drawing.dll and convoluted code (I'm sure it works, but damn its ugly).

GG

like image 246
GG Grubar Avatar asked Feb 06 '26 21:02

GG Grubar


1 Answers

I 'glued' together a bunch of other answers to make a bulk script. Credit to those, but Chrome crashed and I lost those other webpages on Stack. This works on photo files only and will rename all files to YYYYMMDD_HHMMSS.jpg format.

Here it is:

$nocomment = [reflection.assembly]::LoadWithPartialName("System.Drawing")
get-childitem *.jpg | foreach {
    $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 yyyyMd_HHmmss
    $newfilename += ".jpg"
    $pic.Dispose()
    rename-item $_ $newfilename -Force
    $newfilename
}
like image 73
Kevin Avatar answered Feb 09 '26 09:02

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!