×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Recursive

Recursive

Recursive

(OP)
Hello all,

I'm trying to write a script that'll recursively print all the directories to the screen, then go inside those directories and do the same thing primarily, until it can't go any furthur.  

This is what I have now, but his doesn't work at all, and as you can see it's not recursive, If I were to add another directory in at the very end where my opendir function ends, It would only print the directory but wouldn't list everything underneath it.  So can someone help direct me or redirect me to in the right direction...

opendir (DIR, "$dirname") or die "Can't open: $!\n";
@alldir = sort grep(!/^\.¦\.[aA-zZ]/, readdir (DIR)); closedir(DIR);
foreach $dir(@alldir){
     print "$dir";
     opendir(SUB, "$dir") or die "Can't open: $!\n";
     @subdir = sort grep(!/^\./, readdir (SUB));
     closedir(SUB);
          foreach $sub(@subdir) {
               print "$sub";
          }
}

RE: Recursive

If you are on a UNIX box, the find command will give you a good list of all sub dirs and files.


$cmd = 'find /some/path/  -name  "*" ';
@files = system($cmd);
foreach $file (@files) { print "$file\n"; }


This will supply a list of all files and dirs found below /some/path/.

'hope this helps.




keep the rudder amid ship and beware the odd typo

RE: Recursive

(OP)
Im not on a unix box.  Im on an NT machine.  So how do I go about doing htat now.


RE: Recursive

There is a 'File::Recurse' module that does that.....from the ActiveState html version of the perl man pages.....
quote --
The function takes two parameters a function reference and a directory. The function referenced by the first parameter should expect to take one parameter: the full path to the file currently being operated on.  This function is called once for every file and directory under the directory named by the second parameter.
For example:

        recurse(\&func, "/");

would start at the top level of the filesystem and call ``func' for every file and directory found (notincluding ``/').

--end quote

See the perl manpages for more details...




keep the rudder amid ship and beware the odd typo

RE: Recursive

try this:

sub recurse_dirs
{
  my ($dir,$level) = @_;

  opendir(DIR, "$dir") or die "Can't open: $!\n";
  my @alldir=sort grep(!/^\.¦\.[aA-zZ]/,readdir(DIR));    
  closedir(DIR);

  $str = "% $levels";
  $dir = sprintf($str,$dir);

  $level++;
  foreach $dir (@alldir) {recurse_dirs($dir,$level);}
}

recurse_dirs($dirname,0);

I'm not sure if that sprintf hack will work the way I want (print spaces before the directory in order to indent them correctly).  But, you at least get the idea of how to write a recursive subroutine.

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close