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: Date: 6 Nov 93 08:18:27 GMT References: <14510@lhdsy1.lahabra.chevron.com> <1993Nov5.004308.4678@btree.uucp> <14510@lhdsy1.lahabra.chevron.com> <3838@symbas.UUCP> 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 <3838@symbas.UUCP> 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 &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 &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 .