Skip to content

Instantly share code, notes, and snippets.

@sunmockyang
Last active July 25, 2018 05:25
Show Gist options
  • Select an option

  • Save sunmockyang/2201b21de24ff6af545f400b48dfbc6d to your computer and use it in GitHub Desktop.

Select an option

Save sunmockyang/2201b21de24ff6af545f400b48dfbc6d to your computer and use it in GitHub Desktop.
Parse android picture filenames to get the creation time and set the mtime of the file
# Parse android picture filenames to get the creation time and set the mtime of the file
# Example: 20180721_230439.jpg's mtime will be set to 2018-07-21 23:04:39 -0400
require 'date'
require "FileUtils"
dir = ARGV[0]
def get_mtime_from_filepath(path)
filename = File.basename(path, ".*")
year = filename[0 .. 3].to_i
month = filename[4 .. 5].to_i
day = filename[6 .. 7].to_i
hour = filename[9 .. 10].to_i
minute = filename[11 .. 12].to_i
second = filename[13 .. 14].to_i
return Time.new(year, month, day, hour, minute, second)
end
Dir["#{dir}/**/*"].each { |e|
time = get_mtime_from_filepath(e)
FileUtils.touch e, :mtime => time
puts "#{File.basename(e)}'s mtime is now #{time}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment