2010-01-29

使用 EXIF 的時間資料來改檔案名稱

第一版的 script 已經 OK 了,不過考慮到如果有多過於一天的照片,這樣好像還是有點麻煩,所以改寫變成為讀取 EXIF 的資料以後,再用檔案內的日期資料來改檔名。

Tcllib 有提供 jpeg 和 exif 二個套件可以使用,因為 jpeg 有提供範例,所以最後使用 jpeg 套件來實作:
#!/usr/bin/tclsh
#
# Rename script
#
# argument 1: folder location (option)
#

package require jpeg

puts "########## Start ##########"

if {$argc >= 1} {
cd [lindex $argv 0]
} elseif {$argc == 0} {
cd "c:/tmp"
}

foreach filename [glob *.jpg] {
array set exif [::jpeg::getExif $filename]
set today [clock format [clock scan $exif(DateTimeOriginal) \
-format {%Y:%m:%d %H:%M:%S}] -format %Y%m%d]

regsub -all {\mIMG} $filename $today newFileName

file rename $filename $newFileName
}

puts "########## End ##########"

exit

相關資訊:
Wiki:Exchangeable image file format

Exchangeable image file format (Exif) is a specification for the image file format used by digital cameras. The specification uses the existing JPEG, TIFF Rev. 6.0, and RIFF WAV file formats, with the addition of specific metadata tags. It is not supported in JPEG 2000, PNG, or GIF.

2010-01-16

File rename

#!/usr/bin/tclsh
#
# Rename script
#

puts "########## Start ##########"

if {$argc >= 1} {
cd [lindex $argv 0]
} elseif {$argc == 0} {
cd "c:/tmp"
}

set today [clock format [clock seconds] -format %Y%m%d]

foreach filename [glob *.jpg] {
regsub -all {\mIMG} $filename $today newFileName

file rename $filename $newFileName
}

puts "########## End ##########"

exit
會寫這個 script,是因為我的數位相機照完相以後,會是 IMG_0001.JPG, IMG_0002.JPG 這樣編號,而我習慣用日期來整理,所以要把檔名改為 20100116_0001.JPG 這種形式,而自己手動改有點浪費時間,所以寫一個改檔名的 script 來做這件事情。

使用了 glob 來列出所有 *.JPG 的檔案,再來把檔名前半部的 IMG 換成今天的日期,最後改檔名,應該就完成任務了。