http://www.perlmonks.org?node_id=497409


in reply to Finding Oldest File in a Directory

Update: Please note that this solution searches for the files recursively. Thanks to aristotle for the clarification

Use File::Find

Untested

#!/usr/bin/perl -w use File::Find; use strict; my $dir = '/path/to/dir/'; my $oldestfile = 0; my $filename = "No BOT REPORT: ERROR"; finddepth(\&oldfile, $dir); print "oldest file = " , $filename,$/; sub oldfile { return unless (/BOT/); my $mtime = -M $_; if ($mtime > $oldestfile) { $filename = $File::Find::name; $oldestfile = $mtime; } }

BTW i think you are not getting the files using the readdir due to the context

from perldoc -f readdir

readdir DIRHANDLE Returns the next directory entry for a directory opened by "opendir". +If used in list context, returns all the rest of the entries in the d +irectory. If there are no more entries, returns an undefined value i +n scalar context or a null list in list context.