3

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

2
  • Have you checked out this function posted in Microsoft's Script Center? Looks like it does what you need. Commented Aug 7, 2016 at 2:05
  • If you don't want to use System.Drawing to extract the DateTaken attribute, how about using the files' CreationTime property instead? Commented Aug 7, 2016 at 12:38

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.

3 Comments

Thanks for your script! In order for this to work using Windows 10, pwsh 6 core, I had to import as mentioned here: stackoverflow.com/a/54335841/648789
Thank You. This is good change. I updated repo and pushed new package to Powershel gallery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.