-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_photos.rb
More file actions
42 lines (32 loc) · 866 Bytes
/
Copy pathsort_photos.rb
File metadata and controls
42 lines (32 loc) · 866 Bytes
1
2
3
4
5
6
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
39
40
41
42
require 'exifr/jpeg'
require 'fileutils'
class SortPhotos
attr_accessor :target_directory
def initialize
@target_directory = 'sorted'
end
def get_date photo
date_time = EXIFR::JPEG.new(photo).date_time
date_time.to_s[0, 10]
end
def create_directory photo
date = get_date(photo)
directories_created = FileUtils.mkdir_p "#{@target_directory}/#{date}"
directories_created[0]
end
def get_filename photo
photo.split('/')[1]
end
def move_photo photo
date = get_date photo
sub_directory = create_directory photo
filename = get_filename photo
FileUtils.mv(photo, "#{@target_directory}/#{date}/#{filename}")
end
def sort_photos dir
Dir.glob("#{dir}/*.*") do |filename|
next if filename == '.' or filename == '..' or !filename.match(/jpe?g$/i)
move_photo filename
end
end
end