Skip to content

Instantly share code, notes, and snippets.

@jmcnevin
Created January 30, 2011 22:14
Show Gist options
  • Save jmcnevin/803324 to your computer and use it in GitHub Desktop.
Save jmcnevin/803324 to your computer and use it in GitHub Desktop.
Sorts a collection of files into folders based on the show name. I use this for TV episodes.
#!/usr/bin/env ruby -wKU
require 'fileutils'
TV_DIR = File.expand_path("~/Movies/TV")
EP_PATTERN = /^([a-z\.]+)\.((?:S[\d]+E[\d]+)|(?:[\dx\.]+))\..+\.(?:avi|mkv|mp4|mpg|mpeg)$/i
files = Dir[File.join(TV_DIR,'*')]
files.each do |f|
next unless File.file?(f)
base = f.gsub(File.join(TV_DIR,'/'),'')
if base =~ EP_PATTERN
show_name = $1.gsub('.',' ')
show_dir = File.join(TV_DIR,show_name)
dest_path = File.join(show_dir,base)
FileUtils.mkdir_p(show_dir)
FileUtils.mv(f, dest_path)
puts "Moved #{f} to #{dest_path}."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment