Skip to content

Instantly share code, notes, and snippets.

@ktakayama
Created April 27, 2009 03:10
Show Gist options
  • Save ktakayama/102301 to your computer and use it in GitHub Desktop.
Save ktakayama/102301 to your computer and use it in GitHub Desktop.
#! /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