Skip to content

Instantly share code, notes, and snippets.

@dovydasvenckus
Last active April 24, 2019 20:36
Show Gist options
  • Save dovydasvenckus/a702b07e67cab5a38b382b184ba595b9 to your computer and use it in GitHub Desktop.
Save dovydasvenckus/a702b07e67cab5a38b382b184ba595b9 to your computer and use it in GitHub Desktop.
Ruby script that copies music files in flat file hierarchy
#!/usr/bin/env ruby
require 'fileutils'
VALID_FILE_TYPES = ["mp3", "flac", "waw", "aac"]
def print_usage()
puts "music-copy.rb <SOURCE> <DESTINATION>"
end
def validate_args()
if ARGV.length != 2
puts "Invalid usage of script\n"
print_usage()
exit 1
end
if !Dir.exist?(ARGV[0]) || !Dir.exist?(ARGV[1])
puts "Source and destination should be valid folders"
print_usage()
exit 1
end
end
def build_file_search_regex(path, file_type)
File.join(path, "**", "**", "*.#{file_type}")
end
def collect_music_files(source)
VALID_FILE_TYPES.collect {|file_type| Dir.glob(build_file_search_regex(source, file_type), File::FNM_CASEFOLD)}.flatten(1)
end
def should_copy(destination, file_name)
!File.exist?(File.join(DESTINATION, file_name))
end
validate_args()
SOURCE = ARGV[0]
DESTINATION = ARGV[1]
found_music_files = collect_music_files(SOURCE)
found_music_files.each do |file_path|
file_name = File.basename(file_path)
if should_copy(DESTINATION, file_name)
FileUtils.cp(file_path, DESTINATION)
else
puts "#{file_name} already exists on #{DESTINATION}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment