get list of files in subdirectories?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default get list of files in subdirectories?

    Howdy:

    I'd like to get a list of files in a directory and
    also in it's subdirectory - but I don't know how
    far the subdirectory goes.

    I can do a directory like so:

    [snip]
    #my $localdir=cwd;
    my $localdir="/ddrive/db2_text/";
    chdir $localdir;

    opendir (DIR, $localdir) or die "can nae access D drive: $!";
    local @ARGV = grep /\.txt$/, readdir DIR;
    closedir (DIR);

    [/snip]

    But how can I get a list of files in a subdirectory without
    having to specify all of the subdirectories as a variable?
    (I hope that made sense).

    It sounds like I'd have to do something like get a list of
    subdirectories first and then do a search for .txt files
    in each subdirectory?

    Suggestions?

    Thanks!

    -X

    Shaunn Johnson Guest

  2. Similar Questions and Discussions

    1. CFContent accessing subdirectories
      FYI, I've never seen this scenario come up before. Congrats! ::) Normally people ask about serving up PDF, MP3, DOC, etc. files without directly...
    2. referencing an assembly in subdirectories
      Hello I've developed a custom control and compiled it to "/controls/bin/Controls.dll". When i try to reference this control from a page located in...
    3. Forms Authentication on Subdirectories of App
      I would like to have a directory that is Forms authenticated so that all ASP.NET resources contained within the directory require a logged on user....
    4. printing same type of files from mutiple subdirectories of a directory
      Let's say there is a directory called Top. Within the Top directory, there are 26 subdirectories named A-Z. Within each of those A-Z subdirectories...
    5. Apache subdirectories
      Hi, Apache/2.0.44 PHP/4.3.1 WinXP Pro Ive got a site running from c:\apache\htdocs, works perfectly if I make a subdirectory...
  3. #2

    Default Re: get list of files in subdirectories?

    Shaunn Johnson wrote:
    >
    > I'd like to get a list of files in a directory and
    > also in it's subdirectory - but I don't know how
    > far the subdirectory goes.
    >
    > I can do a directory like so:
    >
    > [snip]
    > #my $localdir=cwd;
    > my $localdir="/ddrive/db2_text/";
    > chdir $localdir;
    >
    > opendir (DIR, $localdir) or die "can nae access D drive: $!";
    > local @ARGV = grep /\.txt$/, readdir DIR;
    > closedir (DIR);
    >
    > [/snip]
    >
    > But how can I get a list of files in a subdirectory without
    > having to specify all of the subdirectories as a variable?
    > (I hope that made sense).
    >
    > It sounds like I'd have to do something like get a list of
    > subdirectories first and then do a search for .txt files
    > in each subdirectory?
    Hi Shaunn.

    This is just the sort of job that File::Find was written for.
    Try this short program:

    use strict;
    use warnings;

    use File::Find;

    my $localdir = '/ddrive/db2_text';

    find(
    sub { print $File::Find::name, "\n" if /\.txt$/ },
    $localdir);

    HTH,

    Rob



    Rob Dixon Guest

  4. #3

    Default Re: get list of files in subdirectories?


    On Dec 23, 2003, at 11:52 AM, Johnson, Shaunn wrote:
    [..]
    > But how can I get a list of files in a subdirectory without
    > having to specify all of the subdirectories as a variable?
    > (I hope that made sense).
    >
    > It sounds like I'd have to do something like get a list of
    > subdirectories first and then do a search for .txt files
    > in each subdirectory?
    [..]

    Rob has offered up the classic File::Find solution.

    Otherwise what you will want to do is build a tree
    traverser yourself. You do not actually need to
    chdir() into the directories - but would need
    to remember that when you open the dir_block
    that the 'short names' are not going to be the full path.

    cf:
    <http://www.wetware.com/drieux/pbl/Sys/FS/dir_walk.plx>

    Personally I think going with the File::Find is
    always the better long term solution - unless of
    course one is trying to learn about how to deal with
    things like recursion, and how to solve the classic
    problem of how to check enroute to something else,
    rather than having to solve

    what are the sub_directories I will want to traverse
    so that I can put them in an array...


    ciao
    drieux

    ---

    Drieux Guest

  5. #4

    Default Re: get list of files in subdirectories?

    drieux wrote:
    > On Dec 23, 2003, at 11:52 AM, Johnson, Shaunn wrote:
    > [..]
    > > But how can I get a list of files in a subdirectory without
    > > having to specify all of the subdirectories as a variable?
    > > (I hope that made sense).
    > >
    > > It sounds like I'd have to do something like get a list of
    > > subdirectories first and then do a search for .txt files
    > > in each subdirectory?
    > [..]
    >
    > Rob has offered up the classic File::Find solution.
    >
    > Otherwise what you will want to do is build a tree
    > traverser yourself. You do not actually need to
    > chdir() into the directories - but would need
    > to remember that when you open the dir_block
    > that the 'short names' are not going to be the full path.
    >
    > cf:
    > <http://www.wetware.com/drieux/pbl/Sys/FS/dir_walk.plx>
    >
    > Personally I think going with the File::Find is
    > always the better long term solution - unless of
    > course one is trying to learn about how to deal with
    > things like recursion, and how to solve the classic
    > problem of how to check enroute to something else,
    > rather than having to solve
    >
    > what are the sub_directories I will want to traverse
    > so that I can put them in an array...
    >
    > ciao
    > drieux
    Thanks. I agree on both points. Given the tested and proven
    nature of standard modules, it is definitely more responsible to
    use them in production work. OTOH, the too-quick recourse to
    packaged functionality can short-circuit the development of skills,
    and more importantly, conceptual mastery, for the student. I see
    recursion as an essential concept for developing programmers to
    grasp and integrate, and a directory tree is probably the most
    available practical example of its application. Whatever is
    finally used in production code, the exercise of writing a
    directory tree traversal is worthwhile in itself.

    Joseph

    R. Joseph Newton Guest

  6. #5

    Default Re: get list of files in subdirectories?


    On Dec 24, 2003, at 1:23 PM, R. Joseph Newton wrote:
    > drieux wrote:
    >>
    >> cf:
    >> <http://www.wetware.com/drieux/pbl/Sys/FS/dir_walk.plx>
    [..]

    CAVEAT: this is not a call for the return to 'town/gown' kvetching.
    the fact that I was victimized by latin, greek and hebrew
    in my formative years is not meant to rally 'the old school ties'
    against the heathens of the unwashed masses...
    > OTOH, the too-quick recourse to packaged functionality can
    > short-circuit the development of skills,
    > and more importantly, conceptual mastery, for the student.
    There is also the problem of

    ceteris paribus ( latin for "all things being equal" )

    that swings both ways. Many of the 'best practices' that
    we have are derived from some set of personal experience,
    or faith in the kvetching of another who is alledged to
    have had a good reason for doing it that way.

    If anyone drops that dir_walk.plx into their pet
    perl debugger { which I highly recommend, nothing is
    quite as Frightening a moment as watching what code
    really does, than watching it roll over the rollers
    in the perl debugger... besides building out one's debugging
    skill set will impress your Friends, CoWorkers, and
    GenderAppropriatePerkin }
    they may pause and ask themselves about

    DB<1>
    File::Spec::Unix::canonpath(/System/Library/Perl/5.8.1/File/Spec/
    Unix.pm:46):
    46: if ( $^O =~ m/^(?:qnx|nto|cygwin)$/ && $path =~
    s:^(//[^/]+)(/|\z):/:s ) {
    DB<1>

    and wonder why it is that if they were planning to only
    use the code on a *nix box, they should have made the step
    out to File::Spec on the off chance that it might some day
    be used on a VMS box.

    The problem here of course is that one has either stepped
    off to 'isolate' their OS/FS specific code in a module so
    that only that Module will need to be 'upgraded' to use
    say File::Spec, and/or other OS specific Modules, Or one takes
    the same ceteris paribus approach and opts to buy the
    general case solution to begin with...

    As a part of the ongoing struggle we note that the
    code at
    <http://www.wetware.com/drieux/pbl/Sys/FS/unix_dir_walk.plx>
    differs only from the more generic code with:

    < my $start_dir = "$ENV{HOME}/tmp/Wetware";
    ---
    > my $start_dir = File::Spec->catdir( $ENV{HOME}, qw/tmp Wetware/ );
    62c65
    < my $path = "$dir/$filename" ;
    ---
    > my $path = File::Spec->catfile( $dir, $filename );
    So I am more than willing to support the canonical Jihaud Du Jure
    about the orthodox model for coding...

    {
    subtle sub_text - both codes use
    my @allfiles = File::Spec->no_upwards( readdir DIR );
    }

    MAJOR WARNING:

    there is this minor issue with 'symbolic links' that can
    become nasty - what I like to refer to as the 'jeff foxworthy
    problem'[1][2] - since it is possible to have an entry of the form

    looper -> ../../

    that would create an infinite loop in a simple recursive
    code as originally advertised. This will not be cleaned up
    by simply using File::Spec - and folks should plan to
    deal with this class of issues.
    > I see recursion as an essential concept for developing programmers
    > to grasp and integrate,
    [..]

    I had not intended to get into the
    'jeff foxworthy problem' - since one of the
    hardest functional part of doing 'recursion' is
    making sure that all of the 'exit conditions' are
    cleanly covered....

    But since we were taking the time to step into the
    problem of Production v. Pedagogy I thought I would
    take the time to both raise the issue of 'when is it
    optimization' and that way of course logically leads
    me to the inevitable problem of getting out of logical loops.

    So while it is easier to solve that in the simpler case
    of file system looping, it is less clear how one exits
    gracefully from the problem of 'ceteris paribus',
    all things being equal, except that as one grows out
    their experience they figure out which are their personal
    pet collection of 'inclinations' and sticks to them to
    avoid the problems they know about...


    ciao
    drieux

    ---

    [1] for our non american reader's the gag is

    you might be a redneck if your
    family tree has any loops in it

    tree_traversing needs to be able to manage this
    sort of 'looping' issue - since whether it is a
    file system that allows 'symbolic links' - or any
    other form of hierarchical structure, if the system
    allows for a 'loop'.... well, one needs to resolve
    when it is time to get out of the loop...

    [2] in my next life I get to get out of the habit of footnoting...

    Drieux Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139