2

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

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
GG Grubar
  • 21
  • 1
  • 3
  • Have you checked out this [function](https://gallery.technet.microsoft.com/scriptcenter/Rename-picture-files-with-97738191) posted in Microsoft's Script Center? Looks like it does what you need. – Jon Dechiro Aug 07 '16 at 02:05
  • If you don't want to use `System.Drawing` to extract the `DateTaken` attribute, how about using the files' `CreationTime` property instead? – Mathias R. Jessen Aug 07 '16 at 12:38

3 Answers3

1

Please checkout Set-PhotographNameAsDateTimeTaken Powershell module. It extract date and time from the picture and change name of the picture to it.

Change name of photograph to date time

It allows to use -Recurse -Replace and -Verbose parameter. By default it will create reuslt folder at the same level as your working dir.

If you need change the format of the target names the code can be found here.

Pawel Wujczyk
  • 422
  • 3
  • 12
  • Thanks for your script! In order for this to work using Windows 10, pwsh 6 core, I had to import as mentioned here: https://stackoverflow.com/a/54335841/648789 – marckassay Oct 12 '19 at 18:01
  • Also I had to do the following: https://gist.github.com/marckassay/1676c2ac6d922de221ab1e2ab531ae0b/revisions – marckassay Oct 12 '19 at 18:10
  • 1
    Thank You. This is good change. I updated repo and pushed new package to Powershel gallery. – Pawel Wujczyk Oct 14 '19 at 20:30
0

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
}
Kevin
  • 2,296
  • 21
  • 22
0

In order to avoid this error:

New-Object : Cannot find type [System.Drawing.Bitmap]: verify that the assembly containing this type is
loaded.
...

Make sure the required assembly is loaded before executing the code above:

add-type -AssemblyName System.Drawing
Erwin Zoer
  • 21
  • 5