#!/usr/bin/ruby # coding: utf-8 require 'fileutils' WAKERU = "/" DEVICE_NAME = "" # Dirクラス class Dir # mkdir -p に相当 def self.mkdir_r path base = Dir.pwd path.partition(WAKERU).each do |s| if s != WAKERU if !(Dir.exists? s) Dir.mkdir s end Dir.chdir s end end Dir.chdir base end # ファイル整理 def rearrange! self.each do |file| # 本スクリプトは整理対象から外す if File.absolute_path(file) != File.absolute_path(__FILE__) # ディレクトリは整理対象から外す if !(File.directory?(file)) # 撮影時刻を取得し、パスを生成する time = File.mtime(file) path = time.strftime("%Y") path = path + WAKERU + time.strftime("%Y%m%d_" + DEVICE_NAME) # フォルダを生成する Dir.mkdir_r path # ファイルを移動する path = path + WAKERU + file File.rename file, path end end end end end # カレントディレクトリに対してファイル整理を実行する Dir.new(Dir.pwd).rearrange!.close