Advanced file naming with Lightroom

Learn how to rename your photos with export actions using Lightroom.

Maarten de Boer
Nov 21, 2018 · 5 min read

As a parttime photographer I use Lightroom to edit my photos and PhotoMechanic to import/cull them. Trough the years I've developed a file naming scheme that works for me and contains useful data with near zero chance of duplicates.

For instance it includes the job name (or headlining band of the night) but I want to replace this with the actual band name on export. This caused some unforeseen problems with Lightroom's limited set of renaming options.

In this article I'll explain how I deal with file naming and how to use export actions to deal with special cases.

Renaming your files on import

Having a good file naming scheme is important when importing. It allows you to find and sort files quickly and prevents duplicate filenames (e.g. IMG_1234 Copy 1) when importing from two separate cards. Trust me, it'll happen to you some day and it is a pain to fix. So rename your files on import! But what is a good naming scheme?

For several years I have been using the following scheme:

{sortable date (year month day}-{time (hour month seconds)}-{original file number (4 numbers after IMG_)} {job name}.cr2

Which translates to:

20181120–200057–0099 Test.cr2

The reason I include the original file number — which is the 4-digit number after IMG_ in the original filename — is because together with the date and time it gives a unique filename. Without this number I would still get duplicates as my camera shoots multiple frames per second in burst.

Using Lightroom

When using Lightroom to import your files, use the following filename template.

To enable renaming on import, open the import dialog and open the File Renaming pane on the right side. Check Rename Files and select Edit… to open this dialog.

Configuring the filename template in Lightroom

Using PhotoMechanic

PhotoMechanic has a much more advanced file renaming capabilities than Lightroom so it's quite easy to get it to rename to how we like it.

Simply put this in the Rename Ingested Photos As box:

{datesort}-{timesort}-{fbas:-4} {jobname}

For more info about these image variables and substring extraction please see the PhotoMechanic wiki.

Configuring the filename template in PhotoMechanic

Renaming on export

After editing my photos I add some metadata like keywords and a title. When shooting bands I always put the band name as title. However when we export without renaming it would still show the headlining band in the filename, which makes finding pictures of a specific band in a folder quite a pain in the region somewhere down below.

The solution is to replace the name of the headlining band in the filename with the title from the metadata. But simply replacing some text with another is not possible in Lightroom as it is in PhotoMechanic. And as we use the original file number in the filename which is not in any of the available template fields, we can't replace the whole filename instead.

You'll also see the Filename number suffix template field in the filename template editor dialog will show some garbage data.This is also the case if you used any other metadata that is not available as template field in Lightroom but is available in PhotoMechanic.

To fix this we need to write a script to do the renaming for us, replacing the headlining band with the title from the metadata. This script can be written in any language as long as it can be executed.

The script

I chose to write my script in PHP as I’m a PHP developer and I didn’t feel like fiddling around in bash, grepping my way through data.

In this script I chose to add the length of the longest edge (in pixels) to the filename, ending up with a filename like this when exporting to a 1200x800 file:

20181120–200057–0099 Test_1200.jpg

Have a look and after that I'll explain a bit how it works.

The logic starts at line 52 where it loops over every argument it receives on the commandline. When Lightroom executes the export action it calls the script with each argument being the path to the file that was exported. So with this loop we loop over every file Lightroom has exported and we need to rename.

Next we gather some basic information like the directory the file is in, the filename itself, image dimensions and IPTC metadata to get the title from.

On line 79 the RegEx magic happens that replaces the job title / headlining band with the title from the IPTC metadata and length of the longest edge (in pixels).

And finally on line 82 we rename the file!

To debug your script you’ll need to log to a file as the script is run in the background and no output is shown. In my script I wrote the logMessage function to handle writing to a logfile in the same directory as the script.

Configuring the export action

To configure Lightroom to run the script after exporting, open the export presets dialog and scroll down to the Post-Processing section. At After Export select Open in Other Application and select your script (make sure it is executable).

Configuring the export action

Now export your photos and if everything went well, your files will be renamed shortly after Lightroom has finished exporting.

Final words

In this article I explained how I name my files and how I handle file renaming both during import and export. Though this naming scheme works for me, it might not work for you.

Feel free to modify the script or write your own from scratch and let us know in the comments. I'm looking forward hearing what works best for you personally or if you have found a different and perhaps better way of doing this.

Maarten de Boer Photography

Concert and portrait photographer from Arnhem, the Netherlands

Maarten de Boer

Written by

Photographer / PHP backend developer (Symfony)

Maarten de Boer Photography

Concert and portrait photographer from Arnhem, the Netherlands

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade