How to get list of only directories

Discussion in 'Perl Misc' started by jimnl69@hotmail.com, May 16, 2005.

  1. Guest

    How can I get a list of JUST the first level of directories? Everything
    I try either gives me only . and .. or everything.

    I've tried:
    if ( opendir( DIR, $dir ) ) {
    @dirs = grep( -d "$dir", readdir( DIR ) );
    closedir( DIR );
    @dirs = sort grep( !/^\.\.?$/, @dirs );
    }

    but I get filenames too.
    I've tried:
    my @filesAndDirs = $dh->read();

    but I get filenames too. When I try to filter them out with a -d, all I
    am left with is . and ..

    What I want to do is get a list of all directories. Then go into each
    one of those and get the list of directories there so I can do some
    processing. I don't care about any further down, just the top 2 levels.
    I've gone through every post I can find on directories for the last 2
    weeks and none of them answer the question successfully on getting just
    a list of directories; no filenames and not just . and ..

    Any help greatly appreciated!
     
    , May 16, 2005
    #1
    1. Advertising

  2. wrote:
    > How can I get a list of JUST the first level of directories? Everything
    > I try either gives me only . and .. or everything.
    >
    > I've tried:
    > if ( opendir( DIR, $dir ) ) {
    > @dirs = grep( -d "$dir", readdir( DIR ) );


    Try

    @dirs = grep -d "$dir/$_", readdir DIR;
    -------------------------^^^

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 16, 2005
    #2
    1. Advertising

  3. wrote:
    > How can I get a list of JUST the first level of directories? Everything
    > I try either gives me only . and .. or everything.
    >
    > I've tried:
    > if ( opendir( DIR, $dir ) ) {
    > @dirs = grep( -d "$dir", readdir( DIR ) );


    Unless the contents of $dir is '0' (the digit zero) then the expression "-d
    $dir" will *always* be true so @dirs will contain all entries returned from
    readdir(). It is as if you had written the expression:

    @dirs = grep( 1, readdir( DIR ) );

    You need to use the complete path for stat to determine if it is indeed a
    directory.

    @dirs = grep -d "$dir/$_", readdir DIR;


    > closedir( DIR );
    > @dirs = sort grep( !/^\.\.?$/, @dirs );


    And of course you can combine both filters in one expression:

    @dirs = grep -d "$dir/$_" && !/^\.\.?$/, readdir DIR;


    > }



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 16, 2005
    #3
  4. Joe Smith Guest

    John W. Krahn wrote:
    > wrote:
    >> if ( opendir( DIR, $dir ) ) {
    >> @dirs = grep( -d "$dir", readdir( DIR ) );

    >
    > Unless the contents of $dir is '0' (the digit zero) then the expression
    > "-d $dir" will *always* be true so @dirs will contain all entries
    > returned from readdir().


    Not quite: "-d $dir" will always return true if $dir is a directory,
    even when $dir='0'. A directory is a directory regardless of its name.
    -Joe
     
    Joe Smith, May 17, 2005
    #4
  5. Joe Smith wrote:
    > John W. Krahn wrote:
    >
    >> wrote:
    >>
    >>> if ( opendir( DIR, $dir ) ) {
    >>> @dirs = grep( -d "$dir", readdir( DIR ) );

    >>
    >> Unless the contents of $dir is '0' (the digit zero) then the
    >> expression "-d $dir" will *always* be true so @dirs will contain all
    >> entries returned from readdir().

    >
    > Not quite: "-d $dir" will always return true if $dir is a directory,
    > even when $dir='0'. A directory is a directory regardless of its name.


    Yes, I realised that after I posted which is why I cancelled it. :)


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 18, 2005
    #5
    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. Joel Finkel
    Replies:
    0
    Views:
    494
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    533
    Jeffry van de Vuurst
    Jul 30, 2003
  3. =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=

    Multiple bin-directories with virtual directories?

    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    830
    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=
    Nov 9, 2004
  4. Paul F
    Replies:
    4
    Views:
    638
    Mark Fitzpatrick
    Jun 25, 2008
  5. Adam Petrie
    Replies:
    8
    Views:
    310
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page