Teen Programmers Unite  
 

 

Return to forum top

Directories in perl

Posted by grandsnafu [send private reply] at December 27, 2001, 04:59:03 PM

What's the best way to recurse through directories in perl? Like, if I have the directory structure:

public_html
----comp
--------linux
--------be
--------windows
----random
--------really_random


Assuming each of those is a directory, and each contains some html files. I can do what I want on 'comp' and 'random', but can't figure out how to get down into their subdirectories(i.e. 'linux', 'really_random'). I'm using code like this:

[snip]
my @files = <*>
while(@files) {
my $file = shift(@files);
if (-d $file) {
chdir($file);
[blablablabla]
[snip]

It could probably be done with some bizarre looking, many-times-nested loop, but is there some easier way (it's perl; their has to be, right?)?

Posted by grandsnafu [send private reply] at December 27, 2001, 06:45:16 PM

Maybe there's a unix or perl equivalent of the dos 'tree' command. Anybody know about that?

Posted by gian [send private reply] at December 27, 2001, 10:57:29 PM

I believe that with some formatting of the output from du, it could suit your purposes...

Posted by buzgub [send private reply] at December 27, 2001, 11:13:53 PM

The File::Find module might help.

Posted by taubz [send private reply] at December 28, 2001, 08:36:37 PM

Yes yes yes.... but the *REAL* answer to the question is recursion. (Duh to the two responders.) I'm not sure, grandsnafu, if you're not familiar with the concept or if you just aren't familiar with using subroutines in Perl... so I'll just outline the code.

doMyRecursion(".");

sub doMyRecursion {
my $search = $_[0]; # arguments are stored in @_ array.
my @dirs; # 'my' makes it a local variable
# get a list of subdirectories into @dir
foreach $d (@dirs) {
doMyRecursion(the sub dir to search);
}
# perform some operation on the dir $search
}

So... this will let you take some operation on every directory in the tree. There are a bunch of things left out of this code, so beware that there are pitfalls. See: opendir, readdir, closedir.

- taubz

Posted by gian [send private reply] at December 28, 2001, 10:24:36 PM

* gian is NOT a perl programmer, and therefore cannot contribute further...

Posted by grandsnafu [send private reply] at January 01, 2002, 02:34:02 AM

Umm...thanks taubz, but...were you mocking me? ;-) I couldn't tell. I *do* understand recursion (even mentioned it in the original post), and do understand sub routines. I had a very similar (but more complete) version of what you had, but I wasn't using a while loop with a counter instead of foreach. Don't ask why. So, changed that, but still the original program exists.

I put the absolute path of the dirs into @dirs, and use:
while(<*>) {
# get absolute path, put it in $dirpath
push(@dirs,$dirpath) if (-d $_);
}

I use chdir to get into each directory, and then it does the while(<*>) thing again. It doesn't work too well and probably isn't efficient either.

*a while later* haha...realized that I doing the wrong thing at the wrong time. It should work now. I'll keep you posted.

Posted by grandsnafu [send private reply] at January 01, 2002, 02:46:46 AM

Yep, working perfectly. cool.

Posted by taubz [send private reply] at January 01, 2002, 09:02:25 AM

I wasn't mocking you... tho I guess I misunderstood.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.