Controlling traversal depth using File::Find

Discussion in 'Perl Misc' started by Mark, Mar 16, 2010.

  1. Mark

    Mark Guest

    I am using File::Find to process files in a specified directory tree.
    I would like to be have find() only traverse a top level directory
    (controlling how deep to go would also be helpful).

    This is the best I could do. This code breaks if a relative directory
    path is specified. It seems that File::Find should provide a way to
    specify the maximum depth but I could not find anything.

    use strict ;
    use warnings;
    use File::Find;

    $| = 1;

    my $TopDir = '/temp/sub';
    find({wanted => \&wanted},$TopDir);

    sub wanted {
    if (-d $File::Find::name && $File::Find::name ne $TopDir) {
    $File::Find::prune = 1 ;
    return;
    }

    return if -d;

    # Processing for the file goes here
    print "file: $File::Find::name\n";
    }
    Mark, Mar 16, 2010
    #1
    1. Advertising

  2. >>>>> "Mark" == Mark <> writes:

    Mark> I am using File::Find to process files in a specified directory tree.
    Mark> I would like to be have find() only traverse a top level directory

    Why not just glob it then?

    The whole point of File::Find is because glob is only one level!

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
    Randal L. Schwartz, Mar 17, 2010
    #2
    1. Advertising

  3. Mark

    Mark Guest

    > Why not just glob it then?
    >
    > The whole point of File::Find is because glob is only one level!


    The user will specify, as an input parameter, how deep to traverse a
    directory tree. I was hoping to be able to use File::Find for all
    depths, including the depth of 0. I suppose I could treat depth 0 as
    a special condition and use glob in that case but I would like to
    avoid that if File::Find is capable.
    Mark, Mar 17, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    403
  2. Xavier Decoret

    recursive traversal of file

    Xavier Decoret, Aug 15, 2003, in forum: Python
    Replies:
    2
    Views:
    395
    Paul McGuire
    Aug 19, 2003
  3. AD
    Replies:
    2
    Views:
    1,033
  4. capricious
    Replies:
    2
    Views:
    543
    Jonathan N. Little
    May 30, 2008
  5. gargoyle

    File::Find::find() is depth-first?

    gargoyle, Jan 10, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    211
    Michele Dondi
    Jan 10, 2005
Loading...

Share This Page