# assume you have a $toppath, which is where the traversal starts chdir $toppath or die "can't cd to $toppath: $!"; open( FIND, "find . -type d -print0 |" ) or die "can't run find: $!"; # find will traverse downward from current directory # (ie. $toppath), and because of the "-type d" option, # will only list the paths of directories contained here; # the "-print0" (thanks, etcshadow) sets a null byte as the # string terminator for each file name (don't rely on "\n", # which could be part of a file name). { local $/ = "\x0"; # added thanks to etcshadow's reply while ( my $dir = <FIND> ) { chomp $dir; unless ( opendir( DIR, $dir )) { warn "$toppath/$dir: opendir failed: $!\n"; next; } while ( my $file = readdir( DIR )) { next if ( -d "$dir/$file" ); # outer while loop will handle al +l dirs # do what needs to be done with data files } closedir DIR; # anything else we need to do regarding this directory } } close FIND;
(update: made minor adjustments to comments in the code, added "closedir"; also wanted to point out that the loop over files could be moderated by using "grep ... readdir(DIR)", etc.)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: An alternative to File::Find
by etcshadow (Priest) on Jan 30, 2004 at 06:32 UTC | |
by graff (Chancellor) on Jan 30, 2004 at 06:41 UTC | |
by etcshadow (Priest) on Jan 30, 2004 at 06:44 UTC | |
•Re: An alternative to File::Find
by merlyn (Sage) on Jan 30, 2004 at 12:11 UTC | |
by graff (Chancellor) on Feb 02, 2004 at 02:35 UTC | |
Re: An alternative to File::Find
by crabbdean (Pilgrim) on Mar 04, 2004 at 13:09 UTC |