Wednesday, January 23, 2013

Lightroom folder batch-rename with sqlite

Here's a quick tutorial for doing batch-renames of folders in Lighroom 4.
Warning #1: the tutorial is written for mac, but it would possibly work with minor modifications on Windows if you have the sqlite3.exe binary available, however I cannot test it.
Warning #2: don't trust blindly what I write here, the idea is absolutely correct, but I may make mistakes while I type, or omit some steps, so backup everything before you start and don't do it unless you know what you are doing.

Let' suppose my folders are of the form "2013/January 01" and I want to change them to "2013/01_01". From LR interface, you could right-click on the folder and select "Rename", but if you have thousands of them, it would take forever.

Close LR. Open a text editor, instead, and create a plaintext file (say "print.txt"), enter 12 lines like the following:

SELECT 'mv ' || QUOTE(pathFromRoot) || ' ' || QUOTE(REPLACE(pathFromRoot, 'January ', '01_')) from AgLibraryFolder WHERE pathFromRoot LIKE '%January %';

SELECT 'mv ' || QUOTE(pathFromRoot) || ' ' || QUOTE(REPLACE(pathFromRoot, 'February ', '02_')) from AgLibraryFolder WHERE pathFromRoot LIKE '%February %';

...


Windows users should probably type 'move ' instead of 'mv ' (don't forget the space).
Now create a different plaintext file (say "update.txt"), with 12 lines like this:

UPDATE "AgLibraryFolder" SET pathFromRoot = REPLACE(pathFromRoot, 'January ', '01_') WHERE pathFromRoot LIKE '%January%';

...

Open a terminal prompt and cd to the folder where the catalog lives. Let's assume that the catalog file name is LR4.lrcat.
Enter the following commands:

cat print.txt | sqlite3 LR4.lrcat > batch_rename.sh
bash batch_rename.sh
# check that everything is ok, then:
cat update.txt | sqlite3 LR4.lrcat
rm batch_rename.sh


Windows users should write something like this:


type print.txt | sqlite3 LR4.lrcat > batch_rename.bat
batch_rename.bat
# check that everything is ok, then:
type update.txt | sqlite3 LR4.lrcat
del batch_rename.bat



No comments:

Post a Comment