# convert current time to same format as dirs my $now; { my @now = localtime(); $now = $now[5]+1900; $now[4]++; for ( my $i = 4; $i > 1; $i-- ) { $now .= '_'; $now[$i] = "0$now[$i]"; $now[$i] = substr( $now[$i], -2 ); $now .= $now[$i]; } } # get only those directories that match requirements: my $dir = "parent-of-directories"; opendir my $dh, $dir or die $!; for my $file ( grep Filter( now => $now, dir => $dir ), readdir $dh ) { my $path = "$dir/$file"; # and go right ahead and process the directory "$path" here in the loop # ... # } closedir $dh; sub Filter { # put all filter conditions here ... my %opt = @_; ( -d "$opt{ dir }/$_" ) # is a directory && !/^\./ # is also not hidden/special && /(\d{4}_\d{2}_\d{2}_\d{2})/ # contains date/hour && ($opt{ now } gt $1 ) # that is in the past ; }