7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/photoarchiver.rb', line 7
def self.organize(dir_src, dir_target)
puts "Photoarchiver #{Photoarchiver::VERSION}"
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 "Source Directory: #{dir_src}"
puts "Target Directory: #{dir_target}"
files = Dir.glob(File.join(dir_src, '**', '*')).reject { |temp| File.directory? temp }
photos = files.grep(/\.JPG/i)
photos.each_with_index {|val, index|
date_created = EXIFR::JPEG.new(val).exif.date_time_original || File.stat(val).ctime
FileUtils::mkdir_p File.join(dir_target, date_created.strftime('%Y'), date_created.strftime('%Y-%m-%d'))
target_file = File.join(dir_target, date_created.strftime('%Y'), date_created.strftime('%Y-%m-%d'), date_created.strftime('%Y%m%d-%H%M%S')) + '.jpg'
puts "Moving #{val} to #{target_file}"
File.rename(val, target_file)
}
puts 'Done processing photos'
end
|