gouf
11/4/2015 - 10:33 PM

recfile.rb

require 'fileutils'

class TermColor
  class << self
    # 色を解除
    def reset
      c 0
    end

    # カラーシーケンスを出力する
    def c(num)
      print "\e[#{num}m"
    end

    # 各色
    %i(red green yellow
       blue magenta cyan white).each.with_index(31) do |name, color_code|
      define_method(name) do
        c color_code
      end
    end
  end
end

def scrub_redundant_string(string)
  progname = string
  progname.gsub!(/(\[新\])/, '')
  progname.rstrip!
  progname.gsub!(/^[\s ]+|[\s ]+$/, '')
  progname.gsub!('[終]', '')
  progname
end

def notice_found(progname)
  # ゴニョゴニョ
  TermColor.cyan
  print 'Found:'
  TermColor.white
  puts progname
end

progname_regex = /(.*-((.*)((#|♯).*))\.ts|.*-(.*)\.ts)/
basefolder = 'Z:/録画フォルダ/'
Dir.glob("#{basefolder}*.ts").each do |f|
  basename = File.basename(f)
  capt = basename.match(progname_regex).captures
  progname = capt.values_at(2, 5).compact.first

  unless progname
    puts "No Match: #{basename}"
    next
  end

  # 余計な文字列を削除する
  progname = scrub_redundant_string(progname)
  notice_found(progname)

  # 下準備としてフォルダを必要に応じて作成する
  dist = "#{basefolder}#{progname}"
  FileUtils.mkdir_p(dist) unless FileTest.exist?(dist)

  TermColor.green
  print 'Moving file....'

  TermColor.yellow
  print basename

  # ファイルを移動する
  File.rename(f, "#{basefolder}#{progname}/#{basename}")
  TermColor.magenta
  puts '....Done!'

  TermColor.reset
end