costas has asked for the wisdom of the Perl Monks concerning the following question:

I am currently using the File::Find module but am having trouble finding the answer to the following.

I want to be able to jump to the next directory without going through each file until i hit the next directory. ie...
if (($fileage <= $modtime) && (-d $_)) { #check file age if (($fileage <= $modtime) && (-f $_)){ #..do whatever } } else { #skip to next directory }
can anybody please help?

Replies are listed 'Best First'.
Re: skiping to the next dir using File::Find;
by broquaint (Abbot) on Mar 26, 2002 at 12:01 UTC
    A brief browse of File::Find's documentation suggests that the preprocess option might do the trick
    use strict; use File::Find; find({ preprocess => \&preprocess, wanted => \&wanted, }, "."); # munge data so wanted() gets the desired input sub preprocess { my @files = @_; ... } sub wanted { ... }

    HTH

    broquaint

Re: skiping to the next dir using File::Find;
by cchampion (Curate) on Mar 26, 2002 at 12:04 UTC
    Just a quick comment.
    In your example, you are testing for "-d", and within that condition you are testing again for "-f". That, of course, cannot work. Maybe you should adjust your algorithm.

    Cheers
    cchampion
Re: skiping to the next dir using File::Find;
by vek (Prior) on Mar 26, 2002 at 13:02 UTC
    A quick observation on your code. Why compare $fileage against $modtime twice? The use of the two different file tests is confusing me as well.
    If (($fileage <= $modtime) && (-d $_)) { # at this point we know $_ *must* be a directory if ($fileage <= $modtime) && (-f $_)) { # this -f test won't work because you are # testing $_ again and you've just # established that $_ is a directory
Re: skiping to the next dir using File::Find;
by Util (Priest) on Mar 26, 2002 at 17:37 UTC

    $File::Find::prune almost does what you want. It is normally used to tell File::Find to stop descending the directory tree, but it does not stop File::Find from continuing to walk the directory it is already in. To also abort the current directory, just examine $File::Find::prune in the first line of your wanted subroutine, and return if it is set. File::Find will reset $File::Find::prune when it changes to the next directory.

    HTH,
    Bruce Gray

    sub wanted { # Skip the rest of the current directory. return if $File::Find::prune; if (($fileage <= $modtime) && (-d $_)) { #check file age if (($fileage <= $modtime) && (-f $_)){ #..do whatever } } else { # Do not descend further down the tree. $File::Find::prune = 1; # Skip this file. return; } }