r.joseph has asked for the wisdom of the Perl Monks concerning the following question:
It works, but it only goes through the first branch, and then stops...for example, say this was a directory structure of mine that I was searching:my $root_dir = '/top/directory/to/start/search'; RunDirs($root_dir); sub RunDirs { my $dir = shift; opendir(TMP_D,"$dir") or die "$! opening $dir"; while(my $tmp = readdir(TMP_D)) { $tmp = "$dir/$tmp"; if ($tmp =~ /\.\.?/) { next; } if (-d $tmp) { RunDirs("$tmp"); } elsif ($tmp =~ /^(\w*)\.(jpg|jpeg)$/) { print "$tmp\n"; } } closedir(TMP_D); }
The program would only search through the first tree it hits, then stop. In the above example, it would search this and stop:-| ROOT -|-| SUB 1 -|-|-| SUB 1.1 -|-|-|-| SUB 1.1.1 -|-|-|-|-| SUB 1.1.1.1 -|-|-|-|-| SUB 1.1.1.2 -|-|-| SUB 2.1 -|-| SUB 2 -|-|-| SUB 2.1 -|-|-| SUB 2.2
but it would never get to SUB 1.1.1.2 or SUB 2.1 or SUB 2 or anything, it just stops. I thought that after it had hit the bottom of a certain tree, it would start going back up naturally because of the recursion I used (calling RunDirs multiple times) because the instances of RunDirs() for the higher directories would still be open, and therefore would just grab another entry from readdir(). But obviously, this isn't that is happening? What am I doing wrong - I didn't think directory searching was very difficult, but I guess I am doing something wrong!ROOT -> SUB 1 -> SUB 1.1 -> SUB 1.1.1 -> SUB 1.1.1.1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Procesing directories
by Fastolfe (Vicar) on Jan 05, 2001 at 05:34 UTC | |
|
Re: Procesing directories
by extremely (Priest) on Jan 05, 2001 at 05:36 UTC | |
|
Re: Procesing directories
by myocom (Deacon) on Jan 05, 2001 at 05:29 UTC | |
|
Re: Procesing directories
by chipmunk (Parson) on Jan 05, 2001 at 06:49 UTC | |
by lemming (Priest) on Jan 05, 2001 at 07:11 UTC |