lol lol - 7 days ago 4
Perl Question

Perl: Fastest way to find files older than X number of minutes, sorted oldest to newest

im trying to check if there is a file (i dont care about folders) that older than X minuts. unfortunatly i can;t tell where is my bug on this code.
i will appriciate any help :)

1. Find the files older than X number of minute


my $maindir = "C:\\Users\\Dor\\Desktop\\aba";
my $minutesold = 60;
my $now = time;
my $filedisc;

# Declare arrays
my @xmlfiles;
my @qulfiedfiles;

# Declare a Dictionary
my %filedisc;

opendir(my $dh, $maindir) or die "opendir($maindir): $!";

# Read all the files
while (my $de = readdir($dh))
# get the Full path of the file
my $f = $maindir . $de;

if ( -f $f )

push (@xmlfiles, $f);


# For every file in directory
for my $file (@xmlfiles) {

# Get stats about a file
my @stats = stat($file);

# If time stamp is older than minutes provided
if ($stats[9] >= ($now - (( $minutesold * 60) ))){

# Put the File and Time stamp in the dictionary
print($stats[9] ." .| " .$file ."\n\n");


#print($now ."\n")
#print($now - ( $minutesold * 60) ."\n");


Solving this in kind of a functional programming style is the way to go here I think:

my $dir = shift() || $ENV{HOME}; #command line arg or else home dir
my $minutesold = 60; #1h                                                                                                                                                                                            
opendir my $dh, $dir or die "ERR: opendir($dir) $!\n";

map     "$$_{timestamp}          .|            $$_{file}\n",
#sort   { $$a{timestamp} <=> $$b{timestamp} }  # sort by age
#sort   { $$a{file}      cmp $$b{file}      }  # sort by name
grep    $^T-$$_{timestamp} >= 60*$minutesold,  # $^T is program startup time()
map     {{timestamp=>(stat($_))[9], file=>$_}}
grep    -f $_,
map     "$dir/$_",
readdir $dh;

closedir $dh;