<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">Article 7673 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:7673
Path: feenix.metronet.com!news.utdallas.edu!hermes.chpc.utexas.edu!cs.utexas.edu!math.ohio-state.edu!howland.reston.ans.net!agate!ames!koriel!sh.wide!wnoc-tyo-news!scslwide!wsgw!headgw!cvgw3!tshiono
From: tshiono@cv.sony.co.jp (Toru SHIONO)
Newsgroups: comp.lang.perl
Subject: Re: Tree traversal - call for algorithms
Message-ID: &lt;TSHIONO.93Nov06081827@aquarius.cv.sony.co.jp&gt;
Date: 6 Nov 93 08:18:27 GMT
References: &lt;14510@lhdsy1.lahabra.chevron.com&gt; &lt;1993Nov5.004308.4678@btree.uucp&gt; &lt;Nov.2.18.56.51.1993.27804@pilot.njin.net&gt; &lt;14510@lhdsy1.lahabra.chevron.com&gt; &lt;3838@symbas.UUCP&gt;
Sender: news@cv.sony.co.jp (Usenet News System)
Organization: Sony Corporation, Tokyo, Japan
Lines: 43
Nntp-Posting-Host: aquarius
X-Newsreader: prn Ver 1.07
In-reply-to: hans@symbas.lind.no's message of 4 Nov 93 14:37:55 GMT

In article &lt;3838@symbas.UUCP&gt; hans@symbas.lind.no writes:

: I have a 3 level directory tree with an arbitrary number of files
: in each leaf directory.
: My task is to traverse the tree and examine each file.
:
: I've struggled with readdir and a couple of "for each" loops
: inside each other, but have run into unforeseen trouble.
:
: I assume, however, that this classical task is more elegantly
: solvable by a recursive allgorithm or some perl primitive.
: 
: Any suggestions out there ??

There is a good example named `dodir' in the Camel, page 56.
Or here is a small script which might be easy to start with:

#!/usr/local/bin/perl

&amp;traverse('.');

sub traverse {
    local($dir) = shift;
    local($path);
    unless (opendir(DIR, $dir)) {
	warn "Can't open $dir\n";
	closedir(DIR);
	return;
    }
    foreach (readdir(DIR)) {
	next if $_ eq '.' || $_ eq '..';
	$path = "$dir/$_";
	if (-d $path) {		# a directory
	    &amp;traverse($path);
	} elsif (-f _) {	# a plain file
	    print "$path\n";
	    # or do something you want to
	}
    }
    closedir(DIR);
}
--
Toru "devil-may-care" Shiono          Sony Corporation, JAPAN


</pre></body></html>