#!/usr/local/bin/perl my $NEXT_DAY_HH_THRESHOLD = "08"; #For example, if set to 08, This means that any picture taken before 08AM will be considered part of the previous day -- assuming there were pics that previous day and this is a running set use strict; my $DEBUG=0; #set to 2 to actually see hash tables my $filenameprefix=""; my $filename=""; my $info=""; my $tmpdateinfo=""; my $filenameprefix=""; my $SKIP=""; my $original_filename=""; my $scrubbed_date=""; my $YYYYMMDD=""; my $HH=""; my $MM=""; my $SS=""; my %YYYYMMDD_USED=(); my $newyyyymmdd=""; my $temp=""; use Image::ExifTool 'ImageInfo'; #use Clint::Date; #functions have now been copied into this script so as to not rely on a Clint library while ($filename=<>) { chomp $filename; $SKIP=1; if ($filename =~ /\.jpg$/i) { $SKIP=0; } if ($filename =~ /\.raw$/i) { $SKIP=0; } #no clue if RAW files have EXIF or not if ($filename =~ /\.wav$/i) { $SKIP=0; } #wikipedia says WAV can have exif if ($filename =~ /\.tiff$/i) { $SKIP=0; } #wikipedia says TIFF can have exif if ($SKIP) { next; } $original_filename=$filename; if ($DEBUG) { print "filename is $filename\n"; } $info = &ImageInfo($filename); if ($DEBUG>1) { foreach my $key (sort keys %$info) { print "key=$key, data=$info->$key\n"; } } $tmpdateinfo = $info->{DateTimeOriginal}; if ($DEBUG>0) { print "tmpdateinfo for $filename is \"$tmpdateinfo\"\n"; } $tmpdateinfo =~ /^([0-9]{4}):([01][0-9]):([0-3][0-9]) ([0-2][0-9]):([0-5][0-9]):([0-5][0-9])/; #2009:01:24 23:58:15 if ($DEBUG>0) { print "for $filename, \$1=$1,\$2=$2,\$3=$3,\$4=$4,\$5=$5,\$6=$6\n"; } $YYYYMMDD = $1 . $2 . $3; #YYYYMMDD --- the date $HH=$4; $MM=$5; $SS=$6; #HHMMSS ----- the time $YYYYMMDD_USED{$YYYYMMDD}="1"; #store the date, so if we go past midnight we can "remember" there were pictures before midnight $filenameprefix = $YYYYMMDD; if (($HH >= 00) && ($HH < $NEXT_DAY_HH_THRESHOLD)) { #if it's between midnight and our next-day-threshold (ex: 8AM) if ($DEBUG) { print "** The \$HH of $HH for $filename falls between 00 and $NEXT_DAY_HH_THRESHOLD!\n"; } $newyyyymmdd=&decrement_date_by_one_day($YYYYMMDD); #then we need to figure out what the previous day is in YYYYMMDD format if ($DEBUG) { print "** So we decreament $YYYYMMDD one day to get $newyyyymmdd and check if there were pictures\n"; } if ($YYYYMMDD_USED{$newyyyymmdd} eq "1") { #and if we "remember" there being pictures on that date $filenameprefix = $newyyyymmdd; #then this is a continuance of that event, and we should use that date instead } } if ($filenameprefix ne "") { $filenameprefix .= " - "; } if ($DEBUG>0) { print "fileprefix for $filename is $filenameprefix\n"; } ##### BONUS FUNCTIONALITY; CLEAN UP THE NAME A BIT: $filename =~ s/_IMG.JPG/.jpg/i; print qq[mv "$original_filename" "$filenameprefix$filename"\n]; } ################################################################################################################################# sub decrement_date_by_one_day { #USAGE: ($newyymmdd)=&decrement_date_by_one_day($yymmdd); #USES last_day_of_month; my $date = $_[0]; my $year=""; my $month=""; my $day=""; my $MONTH_DECREMENTED=0; #DEBUG: print "\n\ndate=$date,length(\$date) is ";print length($date);print "\n\n"; #FIRST let us define all the numbers and strings we will need: if (length($date) == 8) { $year = $day = $month = $date; $year =~ s/(....)..../$1/; $month =~ s/....(..)../$1/; $day =~ s/......(..)/$1/; } else { $year = $day = $month = $date; $year =~ s/(..)..../$1/; $month =~ s/..(..)../$1/; $day =~ s/....(..)/$1/; } $day--; #decrement day if (!$day) { #if day is zero, decrement month, set flag $month--; $MONTH_DECREMENTED=1; }#endif if (!$month) { #if month is zero, it's really december of the last year $year--; $month=12; }#endif if ($year == -1) { $year=99; } #if year is -1, it's really 99 if ($MONTH_DECREMENTED) { #if month was decremented, set day to last day of month $day=&last_day_of_month($month,$year); }#endif if ($day =~ /^[1-9]$/) { $day = "0$day"; } #normalize values to have 0 in front of them if less than 10 if ($month =~ /^[1-9]$/) { $month = "0$month"; } if ($year =~ /^[1-9]$/) { $year = "0$year"; } return "$year$month$day"; }#endsub decrement_date_by_one_day ################################################################################################################################# ######################################################################################################################### sub last_day_of_month { #USAGE: ($day)=&last_day_of_month(2,1998); my ($month)=$_[0]; my ($year)=$_[1]; my $day=""; #### months with 31 days: if (($month==1) || ($month==3) || ($month==5) || ($month==7) || ($month==8) || ($month==10) || ($month==12)) { $day=31; #### months with 30 days: } elsif (($month==4) || ($month==6) || ($month==9) || ($month==11)) { $day=30; ### february is special: } elsif ($month==2) { if (($year % 4)==0) { $day=29; } else { $day=28; }#endif it's a leap year } else { $temp = "\n\n******* INTERNAL ERROR MONTH_1_X: month=$month,year=$year *******\n"; $temp .= " (THIS SHOULD NEVER HAPPEN)\n\n"; print $temp; }#endif #print "

Last day of month \"$month\" in year \"$year\" is \"$day\"

\n\n"; # return($day); }#endsub last_day_of_month #########################################################################################################################