in reply to reading directories

This will get all files from the two directories, and place their contents into an array. You can always regex the @filep array for specific extensions after you are done getting the directory listings.

$dir1 = "/foo/bar1" $dir2 = "/foo/bar2" @filep; @fcontents; get_dir($dir1); get_dir($dir2); for (@filep) { my $fc = get_file($_); push @fcontents, $fc; } print "@fcontents"; sub get_dir { my $dir = shift ; opendir(DIR, $dir) || die "Cannot open dir. $!" ; my @files = grep !/^\.\.?$/, readdir DIR ; closedir DIR; for (@files) { $fn = join '/', ($dir, $_); push @filep, $fn; #return @filep ; } sub get_file { my $file = shift; local *IN; open (IN, "<$file") or die "Cannot read '$file': $!"; if (wantarray) { return <IN>; } else { return join '', <IN>; } }

Replies are listed 'Best First'.
Re: Re: reading directories
by Anonymous Monk on Sep 15, 2001 at 21:10 UTC
    what if you want to avoid some files other than the dot files, like some other programs that you may have in the dir. And how would you just get file e.g., file.1,file.2, file.3 etc.

      I'm not sure exactly what you want to do....

      Basically, you have all of your file names in the array @files. You can use a do...for loop

      for (@files) { /\.txt/ && do { print; }; }

      This prints the name of all text files.

      for (@files) { !/\.txt/ && do { print; }; }

      This prints the names all non .txt files (note negation)

      <code> for (@files) { /^ignore?$/ && do { print; }; }

      Will skip all filenames beginning with ignore.

      You should be getting the idea. ~Hammy