require 'exifr' require 'fileutils' module Photoarchiver def self.organize(dir_src, dir_target) if File.exists?(dir_src) && File.directory?(dir_src) Dir.mkdir(dir_target) unless File.exists?(dir_target) else raise 'First argument, source directory, does not exist or is not a directory' end puts dir_src puts dir_target photos = Dir.glob(File.join(dir_src, '**', '*')).reject { |temp| File.directory? temp } photos.each_with_index {|val, index| exif_data = EXIFR::JPEG.new(val).date_time_original FileUtils::mkdir_p File.join(dir_target, exif_data.strftime('%Y%m%d')) target_file = File.join(dir_target, exif_data.strftime('%Y%m%d'), exif_data.strftime('%Y%m%d-%H%M%S')) + '.jpg' puts "Moving #{val} to #{target_file}" File.rename(val, target_file) } puts 'Done processing photos' end end