require 'exifr' require 'fileutils' module Photoarchiver def self.organize(dir_src, dir_target) puts "Photoarchiver #{VERSION}" # Simply check to see if the source directory exists and complain if it does not if File.exists?(dir_src) && File.directory?(dir_src) # Source dir exists, go ahead and make target directory if it does not exist 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 "Source Directory: #{dir_src}" puts "Target Directory: #{dir_target}" # Gather up all the potential photo files photos = Dir.glob(File.join(dir_src, '**', '*.jpg')).reject { |temp| File.directory? temp } # Loop through the photos, not currently using index but may in the future 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'), exif_data.strftime('%Y-%m-%d')) target_file = File.join(dir_target, exif_data.strftime('%Y'), 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