#!/usr/bin/perl # finds date and time embedded in jpg produced by nikon 5700 digital camera. # inserts date time into filename. # file01.jpg becomes file01_YYYY-MM-DD_HH_MM_SS.jpg # will only process .JPG extensions produced by digital camera. # stanley lee # 9/12/2003 morning. # # 11/18/2006 17:25 EST seems to work with Fuji FinePix E900 # script renamed from addDateToFile_nikon to addDateToDigitalCameraPhoto # works with AVI also # # script assumes filename.ext # untested on files with more than one dot or containing other symbols ## # contact: # fusionplant.com/contact/ # variables #$setFileExt='.jpg'; # camera produces this extension, case insensitive foreach $picFile (@ARGV) { # # ignore no extension # $findExt = rindex( uc $picFile, uc $setFileExt); # if ($findExt eq "-1"){ # # if no extension # next; # } # open file, find string in file: date time open (READ, $picFile); ENDHERE: while (){ if ( /(\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2})/ ) { $datetime = $1; last ENDHERE; } } close (READ); next unless $datetime; # cannot find date --> skip $datetime =~ s|(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})|_$1-$2-$3_$4_$5_$6|; $dotPos = rindex($picFile, '.'); $filename = substr($picFile, 0, $dotPos); $fileExt = substr($picFile, $dotPos+1, length($picFile)-$dotPos-1); $renTo= "$filename$datetime.$fileExt"; # print "$picFile ==> $renTo\n"; #testing purposes if (-e $renTo){ warn "cannot rename $picFile to $renTo: file exists\n"; } elsif (rename $picFile, $renTo) { # success } else { warn "rename $picFile to $renTo failed: $!\n"; } }