Adobe Lightroom and exporting to subfolders

For some reason, versions 1 – 3 of Adobe Lightroom cannot export images in a way that mirrors the original structure of your photo library. I can’t offer a reason why other than it was missed by the development and design teams. There’s been enough interest in it apparently that there are more than a few free and pay solutions to the problem.

I looked at a few of the options and for one reason or another, I decided that I didn’t want to use the plug-ins/extensions and came up with a simple solution using a bit of naming trickery. I use this technique for SnugUp.

Here’s what I did in Lightroom version 3.

  1. I selected the photos I wanted to export.  (I generally use the Quick Collection Feature)
  2. File > Export (CTRL + SHIFT + E)
  3. Adjust the Export To option to point to a specific folder of your choosing (in the example below, I set the folder as E:\PhotosBackupJPG
  4. You may optionally put them in a subfolder of your choice (I did not)
  5. Next click the Rename To option and select "Edit…
  6. Clear any value that may already be in the text box below the Example (just highlight the text and press delete).
  7. Then, edit the template to be Folder Name and then Original filename. Select those by using the Image Name grouping. Click the Insert button after each.
  8. Then, I added some text that I knew would be unique to be used as a separator (=-= equals minus equals). To add the text, just click with your mouse  between the two values you just inserted. I know that none of the file names in my library have this exact combination of characters in them. It’s important later. You can confirm this by using the search feature in Lightroom if you’re not sure.
  9. Click on the Preset (yours may say something else) and then click “Save Current Settings as New Preset
  10. Give it a name that you’ll remember (I called mine Folder-Filename) and hit Create.
  11. Adjust the remaining settings per your export needs and begin the process by clicking the Export button.
  12. Wait patiently. Then go do something else as you realize it’s going to take a lot longer than you had expected/wanted.
  13. Now, the next step is the simple trickery. What we’ve done is named all of the files so that they include the folder name as well. So, using a Powershell 2.0 script (Powershell is available for all modern versions of Windows as part of a package download here). So, download it now if you don’t already have it. (You can check for it by looking for Powershell ISE as described in the next step if you’re not sure. You probably have it if you’re using Windows 7).
  14. Start Windows Powershell ISE (in Vista and Windows 7, just type “power” into the Start menu search):
    imageThe reason I suggest the ISE (integrated scripting environment) is that it’s easy to just get stuff running without a lot of hassle.
  15. The ISE will start:
  16. I performed the move/rename in two steps so I could verify things between each step. If you’re familiar with Powershell, feel free to combine them into one step. It’s simple enough. First grab the entire script below and paste it into the top pane (under the tab labeled Untitled1.ps1):
    $root = #"E:\PhotosBackupJPG" $items = Get-ChildItem $root foreach($item in $items) { $path = [regex]::split($item.Name, "(=-=)") if ($path.length -eq 3) { $destPath = $root + "\" + $path[0] if (!(Test-Path -path $destPath)) { New-Item $destPath -type directory "New folder: " + $destPath } Move-Item $item.FullName $destPath } }

    I’ve intentionally left an error in the file so that you’re forced to make the change. Unless you have an “E” drive and the path, “PhotosBackupJPG”, the script needs a tiny modification to make it work.

  17. The first line of the script, $root = #”E:\PhotosBackupJPG” needs to be changed to match with the export location you selected earlier AND also the “#” needs to be removed (it’s a comment character in Powershell script). So, if you exported your photos to C:\Users\Steve\ExportedPhotos, remove the “#” and remove the text inside of the quotes on the first line and replace it with your photo location.
  18. After confirming the location points to where you exported your photos (and videos), hit the Run button (the green arrow in the screen shot below). Or hit the F5 key on your keyboard.


  19. The output (or any errors if you made a typo) appears in the light blue box in the center and when it’s complete, the word “Completed” appears in the status bar at the bottom of the application. It may take a few minutes to complete. I’d suggest looking at your export directory using Windows explorer to confirm everything looks fine before continuing. At this point, you should have some subfolders and all of the files should have been moved into the proper subfolder. Next, renaming.
  20. Click the “New” icon (first icon on left shown above) to create a new Powershell script file (or just replace the existing one, you won’t need it again for this process). In the new file, copy this Powershell script and paste it:
    $root = #"E:\PhotosBackupJPG" $items = Get-ChildItem $root -recurse foreach($item in $items) { $path = [regex]::split($item.Name, "(=-=)") if ($path.length -eq 3) { # $destPath = $root + "\" + $path[0] Rename-Item $item.FullName $path[2] } }

  21. Again, fix the $root to match with the export folder you created.
  22. Confirm it, and hit the Run button.
  23. All of your photos (and videos) will be renamed to only the original file name. The extra folder name and =-= are removed.
  24. Bask in the glory of Powershell and your file wizardy. No plug-ins necessary.

If you’d like to use something other than =-= as the delimiter between the folder and file names, you’ll need to fix the Powershell scripts. The delimiter is on the line:

$path = [regex]::split($item.Name, "(=-=)")

But, unless you understand how Regular Expressions in Powershell work, you may want to avoid this change … it’s not necessarily as simple as just replacing the text.

The scripts above do these things with each file:

  1. Check to see if it has the right pattern
  2. If so, grab the folder name and check to see if the folder exists.
  3. If it does not, create it.
  4. Move the file to the new folder.
  5. Rename the file name, removing the folder and delimiter

If you have questions, please leave a comment.

While this shouldn’t cause any harm, (especially if you just point it at a folder of exported photos and videos, which worse case you just re-export), you use technique and code this at your own risk. I can say I successfully used the script on over 30,000 exported photos without a single problem. Smile