in reply to Re: How do I traverse the files in descending order of modification using File::Find
in thread How do I traverse the files in descending order of modification using File::Find

I thought of it initialy but didnt want to modify the existing code which are is already usin File::Find. If I dont have any choice I will go for it , could you please let me know how I can go about doing that?
  • Comment on Re^2: How do I traverse the files in descending order of modification using File::Find

Replies are listed 'Best First'.
Re^3: How do I traverse the files in descending order of modification using File::Find
by runrig (Abbot) on Apr 03, 2009 at 22:27 UTC
    Save the filenames/times in a hash:
    my %file_times; # Then in the wanted function: $file_times{$File::Find::name} = -M $_;
    Process the files in order of modification time (see sort and How do I sort a hash by value).
Re^3: How do I traverse the files in descending order of modification using File::Find
by almut (Canon) on Apr 03, 2009 at 23:00 UTC

    If you want to avoid modifying existing code too much, you could try something like

    use File::Find; my @files; find(\&collect, "."); sub collect { # 'wanted' function replacement push @files, [ $File::Find::name, (stat)[9] # mtime ]; } sub wanted { # your original 'wanted' function # ... } my @files_by_age = sort { $a->[1] <=> $b->[1] } @files; for my $entry (@files_by_age) { my $fullname = $entry->[0]; my ($dir, $name) = $fullname =~ /(.*?)([^\/]*)$/; # set variables that File::Find would have set $File::Find::name = $fullname; $File::Find::dir = $dir; $_ = $name; # depending on whether you need it, you might want to # chdir() into the directory # ("left as an exercise ..." :) # call wanted() function as File::Find would have done wanted(); }

    (Depending on exactly how you're using File::Find, you might need to finetune the approach... this is just an outline of basic usage.)

      almut:
      I found a very neat way of doing it without modifying any of the existing code, yes I had to change the way I was calling the find.
      Here I go..
      find( {wanted=> \&wanted, preprocess => \&preprocess }, #Added this line thats it '.'); #Added this function as well sub preprocess { my %afiletimes; foreach(@_) { $afiletimes{$_} = -M $_ if -f ; } @_ = sort { $afiletimes{$b} <=> $afiletimes{$a} } keys %afiletimes; }