10月 102011
 

■groovy -v
——————————————————
Groovy Version: 1.8.2 JVM: 1.7.0
——————————————————

■RenameToDateShotJpegFile.groovy


import com.drew.imaging.jpeg.JpegMetadataReader
import com.drew.metadata.Directory
import com.drew.metadata.exif.ExifDirectory

/** Config */
File pictureDir = new File("./pictures")

/** Get Jpeg pictures Path */
List<File> pictures = findPictures(pictureDir, ~/.*\.[jJ][pP][gG]/)

/** Rename to DateShot_FileName */
pictures.each{
  dateShot = getDateShot(it)
  if(!it.getName().contains(dateShot)){
    newPath = it.getParent() + File.separator + dateShot + "_" + it.getName()
    it.renameTo(newPath)
  }
}

def getDateShot(File file) {
  Directory directory = JpegMetadataReader.readMetadata(file).getDirectory(ExifDirectory.class)
  dateShot = directory.getString(ExifDirectory.TAG_DATETIME)
  return (dateShot!=null) ? dateShot.replaceAll(/[ |:\/\\?<>"]/,"") : ""
}

def findPictures(File rootDir, java.util.regex.Pattern filter) {
  List<File> pictures = new ArrayList<File>()
  rootDir.traverse(
    type         : groovy.io.FileType.FILES,
    nameFilter   : filter
  ) { 
      pictures.add( it )
  }
  return pictures
}

■実行
groovy -cp metadata-extractor-2.3.1.jar RenameToDateShotJpegFile.groovy

■参考URL
Groovyでファイルツリーを巡り、最初に見つかったファイルを返却するメソッド書いたよ – No Programming, No Life
http://d.hatena.ne.jp/fumokmm/20110911/1315709484

Javaでjpg画像のexif情報をとってみた|ヘビィ・SMD!
http://ameblo.jp/smd310/entry-10854275741.html

Sorry, the comment form is closed at this time.