Skip to content

Instantly share code, notes, and snippets.

@rubenarakelyan
Created April 28, 2020 15:03
Show Gist options
  • Save rubenarakelyan/c4772a50f8de86278ef3bb1ae979edd1 to your computer and use it in GitHub Desktop.
Save rubenarakelyan/c4772a50f8de86278ef3bb1ae979edd1 to your computer and use it in GitHub Desktop.
Move Lightroom CC original files into album-named folders
require "sqlite3"
require "fileutils"
db = SQLite3::Database.new("database/catalog.db")
# Get list of albums
albums = {}
db.execute("select * from albums") do |album|
albums[album[1]] = album[3]
end
# Get list of assets
assets = {}
db.execute("select * from assets") do |asset|
date = asset[4].split("T")[0]
year = date.split("-")[0]
assets[asset[7]] = { id: asset[7], name: asset[5], old_path: "#{year}/#{date}/#{asset[5]}", new_path: nil }
end
# Get links between albums and assets
db.execute("select * from album_asset_v2") do |album_asset|
assets[album_asset[1]][:new_path] = "#{albums[album_asset[2]]}/#{assets[album_asset[1]][:name]}"
end
# Move all assets to the new path and print any that don't have an associated album
assets.each_pair do |_, asset|
puts "Asset at #{asset[:old_path]} does not have an album and will not be moved" and next if asset[:new_path].nil?
FileUtils.mv(asset[:old_path], asset[:new_path]) rescue Errno::ENOENT
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment