List of directories within a directory

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 "");
push ( [at] filenames, $file);
}
closedir(DIR);

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

Al Moodie.
DFS [ Mi, 30 Januar 2008 03:00 ] [ ID #1920413 ]

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 ( [at] filenames, $file);

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

> }
> closedir(DIR);
>
> but how do I do it for the directories within a directory

my [at] 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
someone [ Mi, 30 Januar 2008 03:30 ] [ ID #1920416 ]

Re: List of directories within a directory

Quoth Al Moodie <nospam [at] nospam.com>:
> I have a directory with 200 sub directories in it. How do I create a
> list of the sub directory names?

Recursively, or just the immediate subdirectories?

> 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 "");

For just the immediate subdirs you need

next if !-d "$dirname/$file";

or possibly

next if -l "$dirname/$file" or !-d _;

depending on your definition of 'directory', or the equivalent with
File::Spec if you want to be more portable (your exclusion of .*
suggests you are on Unix). For finding files (directories are just
files) recursively, use File::Find or one of the more modern
alternatives like File::Find::Rule.

Ben
Ben Morrow [ Mi, 30 Januar 2008 03:26 ] [ ID #1920418 ]

Re: List of directories within a directory

On Wed, 30 Jan 2008 02:30:16 GMT, "John W. Krahn"
<someone [at] example.com> wrote:

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

>>
>> but how do I do it for the directories within a directory
>
>my [at] dir_names = grep -d "$dirname/$_", readdir DIR;

That is what I need. I want to go into each sub directory in turn and
delete specific files.

Thanks also for the general education

Al.
DFS [ Mi, 30 Januar 2008 03:41 ] [ ID #1920419 ]

Re: List of directories within a directory

>>>>> "AM" == Al Moodie <nospam [at] nospam.com> writes:

AM> On Wed, 30 Jan 2008 02:30:16 GMT, "John W. Krahn"
AM> <someone [at] example.com> wrote:

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

>>>
>>> but how do I do it for the directories within a directory
>>
>> my [at] dir_names = grep -d "$dirname/$_", readdir DIR;

AM> That is what I need. I want to go into each sub directory in turn and
AM> delete specific files.

then you should use File::Find or one of the variants. scanning dirs
deeply by yourself has all sorts of little bugs waiting to happen.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
Uri Guttman [ Mi, 30 Januar 2008 05:22 ] [ ID #1920420 ]

Re: List of directories within a directory

Al Moodie <nospam [at] nospam.com> wrote:
>I have a directory with 200 sub directories in it. How do I create a
>list of the sub directory names?

Didn't I just read exactly the same question from a name sake of yours in a
different NG? You may want to read up on the difference between
cross-posting (which is rarely appropriate) and multi-posting (which is
never justified).

Same answer as there: I would use File::Find and prune at a depth of 2.

jue
jurgenex [ Mi, 30 Januar 2008 05:36 ] [ ID #1920421 ]

Re: List of directories within a directory

On Wed, 30 Jan 2008 04:36:32 GMT, Jürgen Exner <jurgenex [at] hotmail.com>
wrote:

>Al Moodie <nospam [at] nospam.com> wrote:
>>I have a directory with 200 sub directories in it. How do I create a
>>list of the sub directory names?
>
>Didn't I just read exactly the same question from a name sake of yours in a
>different NG? You may want to read up on the difference between
>cross-posting (which is rarely appropriate) and multi-posting (which is
>never justified).
>
>Same answer as there: I would use File::Find and prune at a depth of 2.
>
>jue

Sorry, it seemed to me that comp.lang.perl was inactive, hence to
second post here. Now that I know comp.lang.perl.misc it will not
happen again.

Al Moodie.
DFS [ Mi, 30 Januar 2008 15:58 ] [ ID #1920443 ]
Perl » comp.lang.perl.misc » List of directories within a directory

Vorheriges Thema: Get an arbitrary hash key, quickly.
Nächstes Thema: FAQ 1.1 What is Perl?