Skip to content

Instantly share code, notes, and snippets.

@zanloy
Created January 3, 2020 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zanloy/db6afaf4d6e533f4d08e8c1e169b487e to your computer and use it in GitHub Desktop.
Save zanloy/db6afaf4d6e533f4d08e8c1e169b487e to your computer and use it in GitHub Desktop.
[Movey] A simple ruby script to move a movie file to the correct location. #ruby
#!/usr/bin/ruby
require 'colorize'
require 'fileutils'
require 'highline/import'
require 'optparse'
exts = ['.avi', '.m4v', '.mkv', '.mp4', '.srt', '.sub']
# Loop through each ARGV
ARGV.each do |arg|
arg = File.basename(arg)
# If arg is directory
if File.directory?(arg)
# Loop through files in arg dir
Dir.foreach(arg) do |filename|
next if filename == '.' or filename == '..'
path = "#{arg}/#{filename}"
ext = File.extname(filename)
# If not a stoopid mac metadata file and is an acceptable filetype, continue
if filename[0..1] != '._' and exts.include?(ext)
if ['.srt', '.sub'].include? ext
ext = ".en.#{ext}"
end
newname = "#{arg}/#{filename}"
if path != newname
puts "< #{path}".colorize(:blue)
File.rename(path, newname)
puts "> #{newname}".colorize(:green)
end
else
puts "- #{path}".colorize(:red)
FileUtils.rm_rf(path)
end # if exts.include?
end # Dir.foreach
target = "/netshare/videos/movies/#{arg}"
# Check if target directory exists
if File.exists?(target)
# If exists, ask if we want to overwright
a = ''
until %w[y n].include?(a)
a = ask("Target #{target} already exists. Do you want to overwrite it? [Y/n] ") { |q| q.limit = 1; q.case = :downcase }
a = 'y' if a.length == 0
end
next if a != 'y'
FileUtils.rm_r(target)
end # if File.exists?
# Move dir to new home
FileUtils.mv(arg, "/netshare/videos/movies/")
end # if File.directory
end # ARGV.each
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment