On Oct 28, 2:02 am, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
> jordilin wrote:
> > When I read a huge directory with opendir,
> > opendir(DIR,"dirname");
> > my $file;
> > while($file=readdir(DIR))
> > whatever...
> > it loads the oldest ones first. I would like the newest files first,
> > instead of the oldest. Taking into account that I am only interested
> > in the newest files, this takes a lot of time, as the directory is
> > really huge. I am talking about thousands and thousands of files. I
> > need to process the files that are two hours old from now. I am not
> > interested in those older than two hours ago.
>
> Maybe you should let the system do the desired sorting. On *nix that
> might be:
>
> chomp( my @files = qx(ls -t $dir) );
> foreach my $file (@files) {
> last if -M "$dir/$file" > 2/24;
> print "$file\n";
> }
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl
With this code, and taking into account that the directory is huge,
memory usage would be a problem as we are going to use a huge array
@files, and the Unix server is a very important one. Don't know if
that could be achieved by means of a while. The real problem is having
to process many files before arriving to the interesting ones. The
solution would be reading the newest ones first. I think there is no
solution. We have, either to slurp all the files into an array (which
is going to take time and memory), or process the whole directory
through a while (one file at a time) till we get the proper files,
which in this case is going to take a lot of time as well.