List of directories within a directory

List of directories within a directory

am 30.01.2008 03:00:27 von DFS

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 (@filenames, $file);
}
closedir(DIR);

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

Al Moodie.

Re: List of directories within a directory

am 30.01.2008 03:26:43 von Ben Morrow

Quoth Al Moodie :
> 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

Re: List of directories within a directory

am 30.01.2008 03:30:16 von someone

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

Re: List of directories within a directory

am 30.01.2008 03:41:57 von DFS

On Wed, 30 Jan 2008 02:30:16 GMT, "John W. Krahn"
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 @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.

Re: List of directories within a directory

am 30.01.2008 05:22:12 von Uri Guttman

>>>>> "AM" == Al Moodie writes:

AM> On Wed, 30 Jan 2008 02:30:16 GMT, "John W. Krahn"
AM> 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 @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@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 ---------

Re: List of directories within a directory

am 30.01.2008 05:36:32 von jurgenex

Al Moodie 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

Re: List of directories within a directory

am 30.01.2008 15:58:56 von DFS

On Wed, 30 Jan 2008 04:36:32 GMT, Jürgen Exner
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?
>
>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.