なんでこんなスクリプトを書いたのかというと、Ruby + RMagick で Exif 撮影日時にリネーム を使ってリネームしたのはいいけど、元のExif日時が1年前に設定されている写真があったからである。

2008年に撮影したのに、デジカメの設定で1年前の2007年になっていたという。なので、それを解消するために。


#!/usr/bin/env ruby
 
def rename2007(filepath)
  begin
    dir, file = File::split(filepath)
    if file.index('2007') == 0
        s = file
        s[0,4] = '2008'
        return "#{dir}/#{s}"
    end 
    return nil
  end
end
 
ARGV.each{|filepath|
  newfilepath = rename2007(filepath)
  if newfilepath != nil
    # success
    File.rename(filepath, newfilepath)
    puts "rename: #{filepath} -> #{newfilepath}"
  else
    # failure or untouched
    puts "keep: #{filepath}"
  end
}
 
if ARGV.size < 1
  puts "Usage: ruby #{File.basename(__FILE__)} <files>"
end

tags: ruby

Posted by NI-Lab. (@nilab)