2020 Apr. 18.
delredundrecfiles-epgrecfilenamematch.rb
#!/usr/bin/env ruby # encoding: utf-8 # ruby 2.0 # 2018 Jul. 06. # 2014 Feb. 10. # Ryuichi Hashimoto. # # delredundrecfiles-epgrecfilenamematch.rb # epgrecファイル形式でファイル名幹が一致する音声動画ファイルは最小サイズを残し、他は削除する。 # # command -d dir -r # delete larger size av-files of which filenames are same in format of epgrec-file. # -d : dir to search # -r : recursive search # require 'rubygems' require 'bundler/setup' require 'optparse' require 'pathname' require 'Videofile' DIRSEPARATOR = File::ALT_SEPARATOR || File::SEPARATOR @script_name = File.basename($0) # command line check # コマンドライン引数を解析してハッシュに格納する args = {} args[:dir] = '.' OptionParser.new do |parser| begin parser.banner = "Usage: ruby #{@script_name} -d dir -r \nDelete larger audio/video-files same filenames in format of epgrec-file.\n-r : recursively search dir" parser.on('-d DIR','--dir DIR', '-d/--dir DIR -r : switch for recursively seek dir') { |v| args[:dir] = v} parser.on('-r', '--recursive') { |v| args[:recurs] = v} parser.parse!(ARGV ) rescue STDERR.puts(parser.banner) end # argv check if ARGV.length > 0 then begin exit(1) ensure STDERR.puts("Too many arguement.") STDERR.puts(parser.banner) end end end # set search dir if (searchdir = args[:dir]) == nil then begin exit(1) ensure STDERR.puts("You input wrong string to -d/--dir.") STDERR.puts(parser.banner) end end searchdir.slice!(/\/$/) if File.ftype(args[:dir]) == 'link' then if File.ftype(File.readlink(args[:dir])) != 'directory' then STDERR.puts("#{args[:dir]} is not directory.") exit(1) end elsif File.ftype(args[:dir]) != 'directory' then STDERR.puts("#{args[:dir]} is not directory.") exit(1) end STDERR.puts("search-dir : " + searchdir) # get all filenames in directory. # delete larger same stem files. if args[:recurs] then allavfiles = Pathname.glob(searchdir+"/**/*/*{.ts,.mp4,.mp3}") else allavfiles = Pathname.glob(searchdir+"/*{.ts,.mp4,.mp3}") end allavfiles.each do |checkingfilepath| unless checkingvfile = Videofile.init(checkingfilepath) then STDERR.puts("No file.") exit(0) end STDERR.puts(checkingfilepath) if args[:recurs] then checkingvfile.deleteRedundantEpgrecStemFilesInDir(true) else checkingvfile.deleteRedundantEpgrecStemFilesInDir(false) end end STDERR.puts("Finished.")