Skip to content

Instantly share code, notes, and snippets.

@schmidt
Created October 20, 2017 21:11
Show Gist options
  • Save schmidt/b9fbb5987e410eca38e20a4423d8b66c to your computer and use it in GitHub Desktop.
Save schmidt/b9fbb5987e410eca38e20a4423d8b66c to your computer and use it in GitHub Desktop.
Rename images following their proper creation date
#!/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