How to get list of only directories

J

jimnl69

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!
 
G

Gunnar Hjalmarsson

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;
-------------------------^^^
 
J

John W. Krahn

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
 
J

Joe Smith

John said:
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
 
J

John W. Krahn

Joe said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top