Skip to content

Instantly share code, notes, and snippets.

@chrislloyd
Forked from joho/cleanup_album_folders.rb
Created December 18, 2010 10:50
Show Gist options
  • Save chrislloyd/746398 to your computer and use it in GitHub Desktop.
Save chrislloyd/746398 to your computer and use it in GitHub Desktop.
require 'fileutils'
require 'rubygems'
require 'titleize'
def is_artist_folder?(path)
Dir.glob(File.join(path, "*")).any? { |contained_file| File.directory?(contained_file) }
end
def get_artist_name(path)
raw_artist = path.split('-')[0]
if raw_artist
raw_artist.strip.titleize
end
end
def get_album_name(path)
folder_components = path.split('-')
if folder_components[1]
folder_components[1..folder_components.size].join('-').strip
end
end
def ensure_artist_folder_exists(artist_name)
unless File.exist?(artist_name)
FileUtils.mkdir(artist_name)
end
end
def overwrite_artist_and_album(artist, album_name)
puts "ARTIST? (leave blank to remain the same)"
input_artist = gets.strip
artist = input_artist == "" ? artist : input_artist
puts "ALBUM? (leave blank to remain the same)"
input_album = gets.strip
album_name = input_album == "" ? album_name : input_album
return artist, album_name
end
def process_album(artist, album_name, original_path)
puts "About to move to #{artist}/#{album_name} (n to skip, e to edit, anything else to proceed)"
user_action = gets.strip.downcase
case user_action
when "e"
artist, album_name = overwrite_artist_and_album(artist, album)
when "n"
puts "skipping"
return
end
ensure_artist_folder_exists(artist)
FileUtils.mv(original_path, File.join(artist, album_name), :verbose => true)
end
# Main loop from here
Dir.glob("*").reject do |folder|
is_artist_folder?(folder)
end.map do |folder|
artist = get_artist_name(folder)
album_name = get_album_name(folder)
if artist && album_name
process_album(artist, album_name, folder)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment