Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, This script works happily on the folder declared, but how can i read the same file in many subolders. I guess i can out the subfolders in to an array, but the list is vast.
my $folder = '//..'; find (\&process, $folder); sub process { if ( $File::Find::name =~ /\learn$/ ) { open (FILE, $File::Find::name ) or die "Cannot open file: $!"; while ( $line = <FILE> ) { $/= "# input for"; $line =~ s/.....; push(@outLines, $line ); } print to outputfile... } }

Replies are listed 'Best First'.
Re: File::FInd subfolders
by Jenda (Abbot) on May 24, 2004 at 16:27 UTC

    File::Find changes the working directory as it traverses the directory tree and the $File::Find::name contains the complete path from the starting directory. You want to use $_ instead. That is just the filename, without path.

    HTH, Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      sorry, do u mean that it should read $_File....etc?

        No. What I mean is that instead of $File::Find::name you should use $_:

        my $folder = '//..'; find (\&process, $folder); sub process { if ( $_ =~ /\learn$/ ) { open (FILE, '<', $_ ) or die "Cannot open file: $!"; while ( $line = <FILE> ) { $/= "# input for"; $line =~ s/.....; push(@outLines, $line ); } print to outputfile... } }

        Jenda
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
           -- Rick Osborne

        Edit by castaway: Closed small tag in signature