• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Getting list of subdirectories

I would like to build a list of subdirectories? I need to recursively build a list of directories.
0
mdlittle
Asked:
mdlittle
  • 2
1 Solution
 
maneshrCommented:
this should work.
NOTE: if you WANT the recursive listing to go in a symbolic link (assuming you are using UNIX) comment out the line which says

next if (-l $_);


#!/usr/bin/perl

$dir="/www/sbarney/cgi-bin";
print "Listing $dir\n";

push @dir,$dir;

while ($dirs=pop @dir) {
  $whereami=`pwd`;
  chomp($whereami);
  chomp($dirs);
  chdir $dirs;
  print "CHANGING to $dirs\n";
     
  while (<*>) {
    next if ($_ eq ".");
    next if ($_ eq "..");
    if (-d $_) {
      next if (-l $_);
      push @dir,$dirs."/".$_;
    } else {
      print "\t",$_,"\n";
    }
  }
  chdir $whereami;
}
0
 
mdlittleAuthor Commented:
I guess I was not too clear. I just want the top level subdirectories...

Thanks
0
 
maneshrCommented:
change your while to ....

while ($dirs=pop @dir) {
  $whereami=`pwd`;
  chomp($whereami);
  chomp($dirs);
  chdir $dirs;
  print "CHANGING to $dirs\n";

  while (<*>) {
    next if ($_ eq ".");
    next if ($_ eq "..");
    if (-d $_) {
      next if (-l $_);
      print "\t Dir-> ",$_,"\n";
      push @dir,$dirs."/".$_;
    } else {
      print "\t File ->",$_,"\n";
    }
  }
  exit;
  chdir $whereami;
}
0
 
ozoCommented:
If you just want the top level, then what did you mean by recursively?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now