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

Dear monks,

I have a directory containing many new directories all of which contain a file called 'mlc'. I want to be able to read the contents of each 'mlc' file from all directories (115) from with a perl script.

Is this possible? I also want to be able to relate the contents of each file to the name of the directory that it came from.

As there are so many directories I really don't want to hard code it.

All I have so far is listing the files in the directory 'new'.

'new' then contains the 115 directories all containing a file 'mlc' (which all have different contents). All of the directories within new are named the same apart from a number at the end, e.g. dir0 -> dir114

opendir (NEW, "new/") or die "unable to open directory new $!"; my @dir_files = readdir(NEW); print "@dir_files\n";

Replies are listed 'Best First'.
Re: opening directories in perl
by adrianh (Chancellor) on Jul 03, 2005 at 20:33 UTC

    Take a look at File::Find::Rule, which is much easier than File::Find in most instances. You'd want something like (untested):

    use File::Find::Rule; my @mlc_files = File::Find::Rule->file->name( 'mlc' )->in( 'new/' );
Re: opening directories in perl
by TedPride (Priest) on Jul 03, 2005 at 19:35 UTC
    I tested the following on my Mac, so it uses : instead of /, but it should provide the basis for what you need:
    use strict; use warnings; my ($dir, $handle, %mlc, @files, @spider); my $fname = "mlc"; # Main directory to start spidering... push @spider, ':'; while ($dir = shift @spider) { if (-e "$dir$fname") { open($handle, "$dir$fname"); $mlc{$dir} = join '', <$handle>; close($handle); } opendir($handle, $dir); @files = readdir($handle); closedir($handle); for (@files) { push @spider, "$dir$_:" if -d "$dir$_"; } } for (sort keys %mlc) { print "$_\n$mlc{$_}\n\n"; }
    Of course, you could make things much simpler and just cycle through all the directories with numbers 0 through x, where x is the maximum possible value:
    use strict; use warnings; my (%mlc, $handle); for (0..200) { if (-e ":dir$_:mlc") { open($handle, ":dir$_:mlc"); $mlc{"dir$_"} = join '', <$handle>; close($handle); } } for (sort keys %mlc) { print "$_\n$mlc{$_}\n\n"; }
Re: opening directories in perl
by mifflin (Curate) on Jul 03, 2005 at 18:33 UTC
    Take a look at the File::Find module.
Re: opening directories in perl
by trammell (Priest) on Jul 03, 2005 at 18:33 UTC
    You can certainly brew up your own solution with opendir(); one other (possibly easier) solution would be to use module File::Find.
      I'm having some real problems with this File::Find module!

      I have read the relevent docs but am really unsure what to but in the wanted subroutine in order to find the files I want. The files I want to get are new/something_here/mlc

      use File::Find; my $dir = ('new/'); find (\&wanted, $dir); # this bascially lists all the directories, how can I get it to look f +or my 'mlc' files? sub wanted { print $File::Find::dir; }
        In the documentation I find this snippet:
        The wanted() function does whatever verifications you
        want.  $File::Find::dir contains the current directory
        name, and $_ the current filename within that directory.
        $File::Find::name contains the complete pathname to the
        file. 
        
        So I'd try this to start (untested):
        my @mlc; sub wanted { return unless $_ eq 'mlc'; push @mlc, $File::Find::name; } find(\&wanted, '.'); foreach my $file (@mlc) { open(my $fh, $file) or die "Can't open '$file': $!"; # per-file code goes here }