Re: List of directories within a directory
- From: Ben Morrow <ben@xxxxxxxxxxxx>
- Date: Wed, 30 Jan 2008 02:26:43 +0000
Quoth Al Moodie <nospam@xxxxxxxxxx>:
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
.
- References:
- List of directories within a directory
- From: Al Moodie
- List of directories within a directory
- Prev by Date: Re: Odd (undocumented?) behavior of RAM file within a loop
- Next by Date: Re: List of directories within a directory
- Previous by thread: List of directories within a directory
- Next by thread: Re: List of directories within a directory
- Index(es):