Skip to content

Instantly share code, notes, and snippets.

@JeffWaltzer
Last active October 25, 2023 14:31
Show Gist options
  • Select an option

  • Save JeffWaltzer/81bba2afd02d71383e7d to your computer and use it in GitHub Desktop.

Select an option

Save JeffWaltzer/81bba2afd02d71383e7d to your computer and use it in GitHub Desktop.
Script for organizing TV shows
#!/usr/bin/ruby
require 'fileutils'
NO_MOVE_LIST=%w{.directory}
def cleanit(s)
s =s.split('/')[-1]
sub =s.split(/(s[0-9])|([0-9]+of)|([0-9]+x[0-9]+)|(201[0-9])|([0-9]{3})/i)
show_name = sub[0]
if show_name.size > 1
sub = show_name.downcase
else
sub= s.split(/s[0-9]+/)[0]
if sub.size < 0
sub= s.split(/[0-9]+/)
else
sub
end
end
sub.
to_s.
gsub("'", ' ').
gsub('.us.', '').
gsub('.US.', '').
gsub(' Us', '').
gsub(/\W+/, '_').
gsub(/_+/, ' ').
gsub(/_$/, '').
strip.
split(' ').
map(&:capitalize).
join(' ')
end
def all_files
Dir.
glob('**/*', File::FNM_DOTMATCH).
reject { |f| File.directory?(f) }.
reject { |f| NO_MOVE_LIST.include?(f) }
end
def reorg
all_files.map do |f|
cleanit(f)
end.uniq.each do |d|
if d.size > 0 && !File.exists?(d)
puts "Making: '#{d}'"
begin
FileUtils.mkdir_p d if d.size > 1
rescue => e
puts e
end
end
end
all_files.map do |f|
d =cleanit(f)
new_place = File.join(d, File.basename(f))
if d.size >= 1 && f != d && !File.directory?(f) && f.to_s != new_place.to_s
puts "---Moving '#{f}' to '#{new_place}'"
begin
FileUtils.move(f, d)
rescue => e
puts e
end
end
end
end
def clean_dirs
cleaner= `find . -empty -print -type d -exec rmdir {} +`.split.join(' ')
puts "Cleaned empty: #{cleaner}" if cleaner.size>0
end
def clean_junk(pattern)
nfo_cleaner = `find . -name '#{pattern}' -print -delete`.split.join(' ')
puts "Cleaned #{pattern}: #{nfo_cleaner.inspect} " if nfo_cleaner.size>0
end
clean_junk '*.nfo'
clean_junk '*.exe'
clean_junk 'RARBG.com.txt'
clean_junk 'RARBG.txt'
clean_junk 'rarbg.txt'
clean_junk 'Torrent Downloaded from*.txt'
clean_junk 'Torrent Downloaded From*.txt'
clean_junk '2_Eng.srt'
reorg
clean_dirs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment