Re: List of directories within a directory



Al Moodie wrote:
I have a directory with 200 sub directories in it. How do I create a
list of the sub directory names?

I know how create a list of all the files in a directory:

opendir(DIR, $dirname) or die "can't open $dirname: $!";
while (defined($file = readdir(DIR))) {
next if($file =~ m/^\./);
next if($file eq "");

"" is not a valid file name so this test will *always* fail.

$ mkdir ''
mkdir: cannot create directory `': No such file or directory
$ touch ''
touch: cannot touch `': No such file or directory

push (@filenames, $file);

Since you didn't test the file type your @filenames array contains all types including subdirectories.

}
closedir(DIR);

but how do I do it for the directories within a directory

my @dir_names = grep -d "$dirname/$_", readdir DIR;



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.