Created
January 3, 2020 13:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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