in reply to File::find hack

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);