Created
June 25, 2015 20:08
-
-
Save flanger001/f4566861a148f5632c89 to your computer and use it in GitHub Desktop.
A script I wrote to convert files from IMG_XXXX.jpg to YYYY-MM-DD HH.MM.SS.jpg format, with proper duplicate file
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
require 'exifr' | |
files = Dir['*.jpg'] | |
puts "Renaming #{files.count} files..." | |
new_file_names = Hash.new { |h,k| h[k] = [] } | |
files.each do |file| | |
exif = EXIFR::JPEG.new(file) | |
dt = exif.date_time_original | |
new_file_name = "#{dt.strftime('%Y-%m-%d %H.%M.%S')}" | |
if exif.subsec_time_digitized | |
new_file_name += ".#{exif.subsec_time_digitized}" | |
end | |
new_file_names[new_file_name] << file | |
end | |
new_file_names.each do |new_name, old_names| | |
if old_names.count > 1 | |
old_names.each.with_index(1) do |name, i| | |
File.rename(name, "#{new_name} (#{i}).jpg") | |
end | |
else | |
File.rename(old_names[0], "#{new_name}.jpg") | |
end | |
end | |
new_files = Dir['*.jpg'] | |
puts "We now have #{new_files.count} files." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment