Skip to content

Instantly share code, notes, and snippets.

@Tuhaj
Last active September 26, 2015 15:02
Show Gist options
  • Save Tuhaj/ea813dd6f2e5a9a5c4f9 to your computer and use it in GitHub Desktop.
Save Tuhaj/ea813dd6f2e5a9a5c4f9 to your computer and use it in GitHub Desktop.
# For mac XOS
# dependent from SetFile
# uses it like: SetFile -d '12/31/1999 23:59:59' photo.jpg in order to change
# the displayed creation date of a file
# usage: enter directory with all directories with names, which are date-like
# yyyymmdd - so y - year; m - month; d - day
# 1. put file on the desktop
# 2. enter irb
# 3. write in console: load '~/Desktop/change_exifs.rb'
# 4. ChangingPhotoHistory.new.replace_all_created_at_dates
# 5. do nothing, it is done :-)
# if something gets broken, no worries, the displayed on mac created at dates
# are not the "true" one. You can recover them.
class ChangingPhotoHistory
require 'shellwords'
def initialize(main_directory=".")
@main_directory = main_directory
end
def dir_name_to_date(dir_name)
date = "#{dir_name[4..5]}/#{dir_name[6..7]}/#{dir_name[0..3]}"
hour = "23:59:59"
"#{date} #{hour}"
end
def escape_spaces(filename)
Shellwords.shellescape(filename)
end
def replace_all_created_at_dates
Dir.entries(@main_directory).each do |d|
next if is_a_wrong_file?(d)
Dir.entries(d).each do |inner_file|
next if is_a_system_file?(inner_file)
date = dir_name_to_date(d)
file_with_path = d + "/" + inner_file
command = "SetFile -d '" + date + "' "+ escape_spaces(file_with_path)
puts command
system command
end
end
end
def contains_eight_digits(file_name)
file_name =~ /\d{8}/
end
def is_a_wrong_file?(file_name)
file_name.size != 8 or !contains_eight_digits(file_name)
end
def is_a_system_file?(file_name)
file_name == '.' or file_name == '..' or file_name == '.DS_Store'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment