Skip to content

Instantly share code, notes, and snippets.

@criess
Created May 9, 2022 18:15
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 criess/1aa7061d908f3c50f96d64bf0277cbf7 to your computer and use it in GitHub Desktop.
Save criess/1aa7061d908f3c50f96d64bf0277cbf7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# reads filenames from stdin and renames them according to contained
# exif data
#
require 'time'
require 'yaml'
require 'fileutils'
filenames = ARGF.read.strip.split("\n") || []
renames = []
filenames.each do |full|
dirname = File.dirname full
begin
create_date = Time.strptime(`exiftool -createDate "#{full}"`[34..255].strip, "%Y:%m:%d %H:%M:%S")
rescue
begin
# try to use modifydate ....
create_date = Time.strptime(`exiftool -modifyDate "#{full}"`[34..255].strip, "%Y:%m:%d %H:%M:%S")
rescue
# try to use trackCreateDate
create_date = Time.strptime(`exiftool -trackCreateDate "#{full}"`[34..255].strip, "%Y:%m:%d %H:%M:%S")
end
end
extension = full.split('.').last
newfile = create_date.strftime("%Y-%m-%d-%H%M%S") + "_" + `sha224sum "#{full}"`[0..5].strip + "." + extension
renames << [full, File.join(dirname, newfile)] if full != File.join(dirname, newfile)
end
renames.each do |from, to|
puts "rename '#{from}' to '#{to}'"
FileUtils.mv(from, to)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment