Created
October 20, 2017 21:11
-
-
Save schmidt/b9fbb5987e410eca38e20a4423d8b66c to your computer and use it in GitHub Desktop.
Rename images following their proper creation date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'date' | |
require 'fileutils' | |
require 'exif' # brew install libexif; gem install exif | |
Dir['*.{JPG,jpg}'].each do |source| | |
time = | |
begin | |
t = Exif::Data.new(File.open(source)).date_time_original | |
raise Exif::NotReadable if t.nil? || t < '2000' | |
t.sub(':', '-').sub(':', '-') | |
rescue Exif::NotReadable | |
# fallback to modified date, if exif data is not available or unrealistic | |
File.mtime(source).to_s.sub(/ \+0\d00/, '') | |
end | |
target = time.gsub(':', '_') + '.jpg' | |
next if target == source | |
raise "File exists: #{target}" if File.exists?(target) | |
FileUtils.mv(source, target) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment