use strict; use File::Find; use Fcntl; # to get 'em constants use MLDBM qw(DB_File Storable); our $dir = '/home/punkish/stuff'; our $db = '/home/punkish/data/files_by_dates.bdb'; our %o; createDatesDb(); sub createDatesDb { # delete existing db, and create a new one unlink "$db"; tie %o, 'MLDBM', "$db", O_CREAT|O_RDWR, 0640 or die $!; find(\&_buildDatesDb, ("$dir") ); untie %o; } sub _buildDatesDb { if (-f $_) { my @t = localtime( (stat("$_"))[9] ); my $yyyy = $t[5] + 1900; my $mm = sprintf("%02d", $t[4] + 1); my $dd = sprintf("%02d", $t[3]); if (exists $o{$yyyy}) { if (exists $o{$yyyy}->{$mm}) { # year, month, and day exist, so push the file # into the day's array if (exists $o{$yyyy}->{$mm}->{$dd}) { my $aref = $o{$yyyy}->{$mm}->{$dd}; push(@$aref, $_); } # year exists, and month exists, but day doesn't yet exist, # so add it and all other info else { $o{$yyyy}{$mm}{$dd} = [$_]; } } # year exists, but month doesn't yet exist, # so add it and all other info else { $o{$yyyy}{$mm} = {$dd => [$_]}; } } # year doesn't yet exist, so add it and all other info else { $o{$yyyy} = {$mm => {$dd => [$_]}}; } } }