■ ファイル名を exif の撮影日時に変更
※CodeRepos から github へ移ったので、それに応じて内容を書き換えました
デジカメで写真を撮ったりなんかすると、ファイル名が dsc00193.jpg みたいな味気無い感じでイヤです。嫌いです。なので、撮影日時に変更するスクリプトを書いて、github 内の gist へコミットしました。
- rename2exiftime (gist)
引数に指定したファイルの名前を変更します。
$ rename2exiftime photos/*.jpg photos/dsc00193.jpg to photos/2007.12.05_09.56.09.jpg photos/dsc00194.jpg to photos/2007.12.05_09.56.20.jpg photos/dsc00195.jpg to photos/2007.12.05_15.40.47.jpg photos/dsc00196.jpg to photos/2007.12.05_15.41.55.jpg photos/dsc00197.jpg to photos/2007.12.05_16.53.29.jpg
画像ファイル以外が指定されると適当な感じでスキップします。-s を付けて実行すると実際に名前の変更はしないで、変更の内容を画面に出力するだけです。いきなり名前が変わるのが嫌な場合にお勧めです。-q だとエラー以外は画面に出力しなくなります。
スクリプトは以下にも転載しておきますが、gist のファイルが最新です。
#! /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
ヘルプメッセージが凄く手抜き。
Posted by Kyosuke Takayama at 2008-01-02 (Wed) 22:43 printable version


1) ざ (2008-01-12 (Sat) 00:43)
rb ではなく 普通のwindowsのコマンドラインでできるスクリプトやソフトはありませんでしょうか?