Skip to content

Instantly share code, notes, and snippets.

@Gowiem
Created December 4, 2012 02:53
Show Gist options
  • Save Gowiem/4200114 to your computer and use it in GitHub Desktop.
Save Gowiem/4200114 to your computer and use it in GitHub Desktop.
Ruby script to rename video files
#!/usr/bin/env ruby
# vid-utility
shows_folder = "/media/Gowie-Internal/Shared/TV-Shows/"
show_titles = %x(ls #{shows_folder}).split(/\r?\n/)
files = %x(ls)
files.each_line do |filename|
# Make sure we dealing with video files and grab the file_type for later
type_match = filename.match(/.+(.mkv|.avi|.mp4)/i)
file_type = type_match ? type_match[1] : nil
if file_type
show_titles.each do |title|
# Remove the year from the show title as it likely wont be in the filename
subbed_title = title.sub(/(\s*\(.+\))/, '')
if filename.match(/#{subbed_title}/)
# If the filename contains the 'SxxExx' string then use that as the
# new filename, otherwise keep the downloaded filename
typical_filename = filename.match(/([sS]\d{1,2}[eE]\d{1,2})/)
new_name = typical_filename ? typical_filename[1] + file_type : filename
filname = filename.sub(/\n/, '')
destination = "#{shows_folder}#{title}/#{new_name}"#.sub(/\s/, '\ ').sub(/\(/, '\(').sub(/\)/, '\)')
puts destination
move_command = "mv #{filename} #{destination}".delete('\n')
shred = %x[ mv #{filename} #{destination} ]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment