Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

File::Find preprocess difficulties

by Fastolfe (Vicar)
on Jul 13, 2002 at 00:08 UTC ( #181423=perlquestion: print w/ replies, xml ) Need Help??
Fastolfe has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to use File::Find to recurse down a dated directory structure and yell when it finds a certain (the "latest") path/filename within that structure (at the 'year', 'month' or 'day' level).

Running this script with an argument of /path/content, for example, should recurse down a directory tree and print out:

2002/07/14/path/content
if it's able to locate path/content (with any file extension) within the 2002/07/14 directory.

The trick here is that I need the testing done such that the most recent dates are checked first (year 2002 tested before 2001, month 12 checked before 11). The File::Find::Unix man page would seem to indicate that all I need to do is specify a 'preprocess' function to do my sorting. My preprocess function is not getting called and I'm at a loss as to why. The script I'm using appears below (it's small).

use strict; use File::Find; use File::Spec; my $search = File::Spec->canonpath("/".shift()); 1 while $search =~ s!/[^/]+/\.\./?!/!; $search =~ s!^/!!; find({ wanted => \&wanted, preprocess => \&preprocess, # this should do it follow => 1, }, "."); sub wanted { return unless -d; print "testing $File::Find::name\n"; # see these fine if (glob("$_/${search}.*")) { print "$File::Find::name/$search\n"; exit; } } sub preprocess { print "pre-processing @_\n"; # never see this return sort { $b <=> $a } grep { /^\d+$/ } @_; }
Output:
[fastolfe@home test]$ ./resolve-name /path/content testing . testing ./2001 testing ./2001/05 testing ./2001/05/30 ./2001/05/30/path/content
Unfortunately the one it found was actually the oldest, which might imply that it's following a standard ascending sort that one typically sees on Unix.

Comment on File::Find preprocess difficulties
Select or Download Code
Re: File::Find preprocess difficulties
by BrowserUk (Pope) on Jul 13, 2002 at 00:24 UTC

    I'm probably way off here, but didn't I read somewhere that you shouldn't use exit to exit a sub? Wouldn't return; be more useful?


    Anyone know of an abbottoire going cheap?

      This script is actually just one step in a larger process. It's a simplified test case for posting here. A return would continue searching for files, and I'm really only interested in the first one it finds (the latest).
Re: (solved) File::Find preprocess difficulties
by Fastolfe (Vicar) on Jul 13, 2002 at 00:50 UTC
    It appears that when passing follow => 1, File::Find uses a different internal function for walking the filesystem, and this function does not use the preprocess code. Lame.

    Also, it processes the preprocessed list in reverse order, so my sort had to be reversed.

    Thanks anyways..

Re: File::Find preprocess difficulties
by Anonymous Monk on Sep 20, 2013 at 05:43 UTC

    Just a comment... yes I know it is a old thread...

    Wanted is called with the filename of the directory BEFORE it calls the preprocess to sort the contents of that directory.

    This is actually logical, but means preprocess will never be called by your code.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://181423]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-08-30 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls