Your photos are a mess! Maybe this PowerShell script can help

As a family, you have a huge photo problem. Well, at least my family does.

After a holiday we have thousands of photos on two smartphones, two regular cameras, and a few on an iPads as well. From this horrible mess, my wife wants to select the best of the best and print them in a photobook.

Before she can start putting the photobook together, we need to collate the photos from all the devices in such a way that photos are in precise chronological order, regardless of which camera they came from. And to make matters worse, the smartphones have automatically adjusted for timezones, while the others have not.

Enter the "Rename-Photo" commandlet I put together in Powershell. This thing has improved my life far more than the average PowerShell script.

Disclaimers:

First: if you're going to use this, there is no warranty, and I advise testing on one or two pictures first, before blasting all of your photos.
Second: I'm not a great PowerSheller and haven't turned this into a reusable "module". If you want to help me turn it into a module please do!

With those caveats out of the way, I'll show you why I love this script and why it brings bliss to my whole family.

First, you bring all the photos together, from all the devices, onto one hard drive, in separate folders. This might itself involves lots of rummaging for cables and some cursing and various levels of frustration. But eventually you might have a structure like this, with one folder for each device:

photo folders

You can grab the script from here:

Here's the script:

github/secretGeek/rename-photo

or by running:

git clone https://github.com/secretGeek/rename-photo.git

And to make sure the Rename-Photo function is available, "dot source" the PowerShell file, by running:

. .\RenamePhoto.ps1

(from Powershell!)

Done that? Now go into one of your photo folders (in PowerShell) and run the rename-photo function against all the files. You'll need to know what values to provide for a few snazzy parameters.

For example

dir *.jpg | % { Rename-Photo $_.FullName "iPhoneSally" "Martinique" }

This will rename all of the jpg files in the current folder, to have a filename like this:

2017-11-10-14-12-10_iPhoneSally_Martinique_53959339.jpg

Where:

  • 2017-11-10-14-12-10 — is a sortable representation of the date the photo was taken (from the Date taken EXIF data). It's not formatted in strict ISO 8601 format. If the file doesn't have the relevant EXIF data (for example if it's a png file) then the LastWriteTime is used instead.
  • iPhoneSally — is the device name you've supplied as a parameter. This can be helpful later if you need to work out who took a particular photo
  • Martinique — is from the 'location' parameter
  • 53959339 — is the number of bytes of the file, and acts as a useful tiebreaker in-case two photos were taken on the same device in the same second. (This was effective for me, over tens of thousands of photos, so I wasn't forced to use something like a checksum for tie-breaking.)

There's one more parameter, and you can use it for fixing the infamous timezone issue. Once you've worked out that the camera's internal clock was reporting times that were 10 hours earlier than the real local time, then add a final parameter of "10", e.g.

dir *.jpg | % { Rename-Photo $_.FullName "iPhoneSally" "Martinique" 10}

If you've made a small mistake, the script can be re-run. There's no limit on how many times you can re-name the same file. And you can alter the script to improve the format.

The real filename formatting work is done in the line that looks like this:

$newName = ("{0:yyyy-MM-dd-HH-mm-ss}_{1}_{2}_{3}{4}" -f $exactDate, $device, $location, $length, $extensionWithDot)

And if you change the script, just dot it again. There's no limit on the number of times you can do this either.

Once you're happy with the filenames, copy all of the files, from all of the devices, together into a single folder.

I tend to break them down into one folder for each year, so you don't totally overwhelm the file system.

Then all the files are together, and the joyous hurdy gurdy of a family trip has been sorted into a single stream of captured moments.

Any improvements, please submit a pull request or raise an issue on GitHub.

Any questions, the comments are open.

kind regards
lb

 

I'm currently writing a book about how to build your first product. If you want to build your first product, please sign up to be notified when the book is available.

(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.