EOS RAW(CR2)とJPEG(JPG)の撮影日付けでファイルを整理
写真ファイルを整理するのに、ファイル名に撮影日付とキーワード、場所を入れている。 最初のときは、日付を「年月」だったので、今の「年月日」形式にファイル名を一括変換するプログラムを開発した。
一日で作成したので、エラー処理は適当。 自己責任でご使用ください。 Windows用です。Windows7で動作確認しています。
使用方法は、リクエストがあれば説明します。 なお、rubyによるプログラムで、ライブラリに、exifr が必要です。 EOS RAW(CR2)の撮影日付をexifrで取得しています。
# -*- coding: shift_jis -*- # # rename cr2/jpg files by photo taken date # require "exifr" def do_rename( mode, files ) return nil,nil,nil,nil if files.nil? or files.empty? if mode == 'R' rename = true else remame = false end total = 0 skipped = 0 right_name = 0 renamed = 0 renamed_log = [] right_log = [] skipped_log = [] files.each{|f| dir = File.dirname( f ) ext = File.extname( f ) fn = File.basename( f, ext ) case ext.downcase when '.jpg','.jpeg' exif = EXIFR::JPEG.new( f ) date = exif.date_time_original # note, return nil when no exif data when '.cr2' exif = EXIFR::TIFF.new( f ) date = exif.date_time_original else next end # validate date if date.nil? skipped += 1 skipped_log << "SKIPPED(noDATE) #{fn+ext} #{dir}" print 'N' next end # name = date.strftime( "%y%m%d" ) if fn[0..5] == name # YYMMDD right_name += 1 right_log << "SAME #{name} - #{fn+ext} #{dir}" print '-' elsif fn[0..3] == name[0..3] # YYMM newfn = name + fn[4..-1] newpath = dir + '/' + newfn + ext # check file already exist if File.exist?( newpath ) skipped += 1 skipped_log << "SKIPPED(EXIST) #{newfn} - #{fn+ext} #{dir}" print 'E' next end # rename File.rename( dir + '/' + fn + ext, newpath ) if rename renamed += 1 renamed_log << "RENAMED(YYMM) #{newfn} - #{fn+ext} #{dir}" print 'r' else newfn = name + '~' + fn newpath = dir + '/' + newfn + ext # check file already exist if File.exist?( newpath ) skipped += 1 skipped_log << "SKIPPED(EXIST) #{newfn} - #{fn+ext} #{dir}" print 'E' next end # rename File.rename( dir + '/' + fn + ext, newpath ) if rename renamed += 1 renamed_log << "RENAMED(else) #{newfn} - #{fn+ext} #{dir}" print 'R' end } # close save_log( '_renamed.log', renamed_log ) save_log( '_right.log', right_log ) save_log( '_skipped.log', skipped_log ) if rename status = '*RENAMED* ' else status = '*LISTUP* ' end return "#{status} Renamed : #{renamed}, right name : #{right_name}, skipped : #{skipped}", renamed_log, right_log, skipped_log end def save_log( logfile, log ) return if logfile.nil? or logfile.empty? log = [ 'ERROR: nil contents.' ] if log.nil? logfile = @log_dir + '/' + logfile File.delete( logfile ) if File.exists?( logfile ) File.open( logfile, 'w' ) { |file| file.write "log file\n\n" log.each { |l| file.write "#{l}\n" } file.write "\n.\n" } end ### get files require 'find' def listup_files( dir_name ) return nil,nil if dir_name.nil? or dir_name.empty? files = [] jpg = 0 cr2 = 0 Find.find( dir_name ) { |f| #dir = File.dirname( f ) ext = File.extname( f ) #fn = File.basename( f, ext ) case ext.downcase when '.jpg','.jpeg' files << f jpg += 1 when '.cr2' files << f cr2 += 1 end } return nil,nil if files.empty? return "Total #{jpg+cr2} files (jpg:#{jpg}, cr2:#{cr2})", files end ### main print "cr2 jpg renamer\n" print "rename to YYMMDD in filename top\n" print "stage: development\n" print "short instraction" # init @log_dir = File.dirname( $PROGRAM_NAME ) # there's no '/' at last # loop while true print "\nEnter dir name or just enter for exit = " # dir_name = STDIN.gets.chomp if dir_name.nil? or dir_name.empty? break end # get files status, files = listup_files( dir_name ) if status.nil? print "\nNO tartet files\n" redo end print status + "\n" print "samples:\n" files.sample(5).each {|f| print f + "\n" } print "\nR for rename, L or other for listup, enter only for restart = " key = STDIN.gets.chomp redo if key == '' # execute rename status, renamed_log, right_log, skipped_log = do_rename( key, files ) if status.nil? print "\nNO affected files\n" else print "\n" + status + "\n" print "renamed (#{renamed_log.size}) samples:\n" renamed_log.sample(5).each {|f| print f + "\n" } print "right (#{right_log.size}) samples:\n" right_log.sample(5).each {|f| print f + "\n" } print "skipped (#{skipped_log.size}) samples:\n" skipped_log.sample(5).each {|f| print f + "\n" } end # loop end print "\nprogram is terminated.\n" exit # end of program
作成: 2013年2月27日
更新: 2013年2月27日
カテゴリ: 写真 日記 cr2renamer