Created
December 18, 2010 01:12
-
-
Save joho/745993 to your computer and use it in GitHub Desktop.
takes all your "artist - album" folders, and makes them "artist/album" instead
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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