Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem readinf directories

Status
Not open for further replies.
Jun 19, 2001
86
US
The following code looks for directory names in a given directory and pushes those names onto an array. It works fine unless the directory name contains the number 1. If it contains the number 1 the name doesn't get pushed onto the array.

Nathan

### Get list of subdirectories in base directory ###
opendir(DIRHANDLE, "$b_directory") or die "$!";
while(defined($dirname = readdir(DIRHANDLE)))
{
#get rid of . and ..
next if $dirname =~ /^\.\.?$/;
#make sure we are looking at directorys
next if $dirname =~ -d "$b_directory/$dirname";
#push directory name onto and array
push(@dirs, $dirname);
}
closedir(DIRHANDLE);
 
Well this example will give you all directories and sub-directories. Not sure if this is what you want though.

use File::Find;

my @FileList;
my $drv = 'some folder name';

find( sub { $FileList[++$#FileList] = $File::Find::name if (-d) }, $drv);


Hope this helps !
 
That will probably work but I'd rather not use a module. I'm also curious to know why my example ignores any directory with a 1 in the name.

Nathan
 
Well this is what the problem was :

change this :

next if $dirname =~ -d "$b_directory/$dirname";

to:

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

Works just fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top