Skip to content

Instantly share code, notes, and snippets.

@flanger001
Created June 25, 2015 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flanger001/f4566861a148f5632c89 to your computer and use it in GitHub Desktop.
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
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