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!

reading directories 1

Status
Not open for further replies.

jimberger

Programmer
Jul 5, 2001
222
GB
hi all,

How do i read an directory and return all the directores inside that root directory?

jmi
 
This probably isn't the slickest implemenation but it
works:


$dir = "/home/gmccone/";
opendir( DIRS, $dir );
foreach $dirEntry ( readdir DIRS )
{
push( @subDirs, $dirEntry ) if -d ($dir . $dirEntry);
}
closedir DIRS;
printf "Subdirectories:\n %s" , "@subDirs";


I would make a subroutine to house this and just call that.
 
Here's another way of doing it using map. I've just discovered map and am enjoying finding all sorts of ways to use it.

opendir(DIR, "/home/readey");

$dirs = [readdir(DIR)];

map {print $_ . "\n" if -d $_} @$dirs;
 
ok.... <suspicious look> what does [tt]map()[/tt] do then? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
No no no... use the globerific abilities of perl!
#==========================
sub get_dirs {
$path = &quot;/some/path/*&quot;;

while(< $path >){
push @dirs, $_ and print &quot;$_ -> DIR\n&quot; if -d $_;
}

return @dirs;
}
#==========================

--Jim
 
map takes an array argument and maps an operation to every member of the array. So in my example,

$dirs = [readdir(DIR)]; # $dirs is an array reference created by enclosing the array returned by readdir(DIR) in square brackets. Then we want to print out each member of @$dirs if it is a directory so the map statement prints out a member of @$dirs if it is a directory, so,

map {print $_ . &quot;\n&quot; if -d $_} @$dirs;

Cute eh?

It is particularly useful if you want to print lists from the command line so,

perl -e 'map {print $_ . &quot;\n&quot;} @INC'

will print out the dirs in @INC without having to code a for / foreach loop. If you had an array (@nums) with values e.g. 1 to 9 and wanted to add, say, 5 to each one you could have something like

@summed = map {$_ + 5} @nums;
map{print $_ . &quot;\n&quot;} @summed;

Luverly jubbly eh?
 
map takes an array argument and maps an operation to every member of the array. So in my example,

$dirs = [readdir(DIR)]; # $dirs is an array reference created by enclosing the array returned by readdir(DIR) in square brackets. Then we want to print out each member of @$dirs if it is a directory so the map statement prints out a member of @$dirs if it is a directory, so,

map {print $_ . &quot;\n&quot; if -d $_} @$dirs;

Cute eh?

It is particularly useful if you want to print lists from the command line so,

perl -e 'map {print $_ . &quot;\n&quot;} @INC'

will print out the dirs in @INC without having to code a for / foreach loop. If you had an array (@nums) with values e.g. 1 to 9 and wanted to add, say, 5 to each one you could have something like

@summed = map {$_ + 5} @nums;
map{print $_ . &quot;\n&quot;} @summed;

Luverly jubbly eh? :)
 
ok.... <sound of brain overheating>

I'm going to look a the perldoc stuff - looks good :) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
somebody who kind enough to look at this...please.

Code:
#read from Location.pm module which return #location of the databases.

@filelocation = Location::destination ($database);

  $location = $filelocation[0];

  chdir $location;

  opendir (DIR, &quot;$location&quot;)|| die &quot;Directory not found\n&quot;;

  @fileList = readdir(DIR);
  foreach my $file (@fileList) {

   #...do something with the files

  }

the problem is..the foreach loop keeps on going.i'm not sure if there's something wrong with my loop..or did i misused the 'chdir' function?..thanks a lot...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top