Home > Archive > PERL Miscellaneous > January 2008 > List of directories within a directory
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
List of directories within a directory
|
|
| Al Moodie 2008-01-29, 10:18 pm |
| 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.
| |
| John W. Krahn 2008-01-29, 10:18 pm |
| 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
| |
| Ben Morrow 2008-01-29, 10:18 pm |
|
Quoth Al Moodie <nospam@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
| |
| Al Moodie 2008-01-29, 10:18 pm |
| On Wed, 30 Jan 2008 02:30:16 GMT, "John W. Krahn"
<someone@example.com> wrote:
>Al Moodie wrote:
[color=darkred]
>
>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.
| |
| Uri Guttman 2008-01-30, 4:29 am |
| >>>>> "AM" == Al Moodie <nospam@nospam.com> writes:
AM> On Wed, 30 Jan 2008 02:30:16 GMT, "John W. Krahn"
AM> <someone@example.com> wrote:
[color=darkred]
[color=darkred]
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 ---------
| |
| Jürgen Exner 2008-01-30, 4:29 am |
| Al Moodie <nospam@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
| |
| Al Moodie 2008-01-30, 7:18 pm |
| On Wed, 30 Jan 2008 04:36:32 GMT, Jürgen Exner <jurgenex@hotmail.com>
wrote:
>Al Moodie <nospam@nospam.com> wrote:
>
>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.
|
|
|
|
|