Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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:

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+$/ } @_; }
[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.

In reply to File::Find preprocess difficulties by Fastolfe

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (5)
    As of 2021-02-26 00:34 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found