I tried a different approach. Unfortunately it didn't work out as well as I would have hoped - but I'd never played with the preprocess option before. However along the way, I found out the finddepth() entry point or bydepth=>1 option didn't work on my Windows XP machine. So mileage does vary on this point! Thought I'd pass that tid-bit along since you appear to be on Windows.

I also found out that this pre-process option as well as ordering/filtering files, can also evidently be used to "prune" off sections of the tree to excluded from further searching. It modifies the output of readdir(), but before calling the wanted() subroutine - so if a directory name is filtered out, it won't follow that branch.

Also on Windows NTFS, I am unsure whether this "atime" last access time is really available or not. "mtime" (-M) or last modified time is portable between Unix and Windows. Also, ISO-9660 (CD-ROM) can be problematic if you want the code to work on that file system also. But I think that -M works on all of the these.

Overall, I'm not too happy with this approach - if the bydepth option had worked - the code would be shorter - but it does illustrate some points that may be useful on another project and so, I'll pass it along...

#!/usr/bin/perl -w use strict; use File::Find; my @dirs = ('C:/temp'); my %options = (preprocess =>\&newest, wanted=>\&wanted, bydepth=>1); # bydepth didn't work on Windows XP!! find (\%options, @dirs); sub newest { # print "processing $File::Find::dir for ".@_." entries\n"; # takes a list and returns a list of directories/files for # further processing.. my $newest_file; my @files; foreach (@_) { if (!-f) {push @files, $_} # links,directories elsif (/\.txt$/) { $newest_file ||= $_ and next; $newest_file = $_ if (-M > -M $newest_file); } } unshift (@files, $newest_file) if ($newest_file); return(@files); } sub wanted { return if -d; print "$File::Find::name\n"; #only one "newest .txt file!" } __END__ Newest .txt file in each directory... C:/temp/10.txt C:/temp/inline/_Inline/build/_24to32/Comments.txt C:/temp/Martin-GermanSchoolProject/output.txt C:/temp/students/debug_notes.txt
Update: Performance notes: When Perl does a file test operation, it caches the results of the stat() operation. The special file testing variable, "_", just plain old underscore, not $_ in the second version says to just access this cached value from the -f file test previously. And of course since a file test operation is "expensive" if the program cached the -M $newest_file value, it would be faster. None of this usually matters, but once you get to say 1,000 files, it will start "mattering".
$newest_file = $_ if (-M > -M $newest_file);
$newest_file = $_ if (-M _ > -M $newest_file);

In reply to Re: File::find hack by Marshall
in thread File::find hack by nemesdani

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.