Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#! /usr/bin/env ruby
require 'rubygems'
require 'extexif'
require 'optparse'
def main
simulation = nil
quiet = nil
help = nil
OptionParser.new{|opt|
opt.on('-s') { simulation = true }
opt.on('-q', '--quiet') { quiet = true }
opt.on('-h', '--help') { help = true }
}.parse!(ARGV)
if help
usage
exit
end
ARGV.each do |f|
next unless(File.file?(f))
begin
img = ExtExif.new(f)
new_name = img['DateTimeOriginal'].gsub(/:/, '.').gsub(/\s/, '_')
if(new_name !~ /^[\d\._]+$/)
raise f + ": Could not read exif data."
end
new_name = File.join(File.dirname(f), new_name + File.extname(f))
if(File.exists?(new_name))
raise f + ": " + new_name + ' is already exists.'
end
puts f + ' to ' + new_name if(quiet.nil?)
File.rename(f, new_name) if(simulation.nil?)
rescue => err
puts err
end
end
end
def usage
puts <<-EOF
-h,--help show this message.
-q,--quiet quiet mode.
-s only simulation.
EOF
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment