13

I usually use something like

my $dir="/path/to/dir";
opendir(DIR, $dir) or die "can't open $dir: $!";
my @files = readdir DIR;
closedir DIR;

or sometimes I use glob, but anyway, I always need to add a line or two to filter out . and .. which is quite annoying. How do you usually go about this common task?

Mark Canlas
  • 9,385
  • 5
  • 41
  • 63
David B
  • 29,258
  • 50
  • 133
  • 186
  • 7
    From the explosion of responses, you can see that It Entirely Depends on What Results You Want to Get. :) – Ether Sep 22 '10 at 17:42
  • Possible duplicate of *[How do I read in the contents of a directory in Perl?](http://stackoverflow.com/questions/22566/how-do-i-read-in-the-contents-of-a-directory-in-perl)*. – Peter Mortensen Jan 08 '14 at 10:56

6 Answers6

14
my @files = grep {!/^\./} readdir DIR;

This will exclude all the dotfiles as well, but that's usually What You Want.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
9

I often use File::Slurp. Benefits include: (1) Dies automatically if the directory does not exist. (2) Excludes . and .. by default. It's behavior is like readdir in that it does not return the full paths.

use File::Slurp qw(read_dir);

my $dir = '/path/to/dir';
my @contents = read_dir($dir);

Another useful module is File::Util, which provides many options when reading a directory. For example:

use File::Util;
my $dir = '/path/to/dir';
my $fu = File::Util->new;
my @contents = $fu->list_dir( $dir, '--with-paths', '--no-fsdots' );
FMc
  • 41,963
  • 13
  • 79
  • 132
8

I will normally use the glob method:

for my $file (glob "$dir/*") {
    #do stuff with $file
}

This works fine unless the directory has lots of files in it. In those cases you have to switch back to readdir in a while loop (putting readdir in list context is just as bad as the glob):

open my $dh, $dir
    or die "could not open $dir: $!";

while (my $file = readdir $dh) {
    next if $file =~ /^[.]/;
    #do stuff with $file
}

Often though, if I am reading a bunch of files in a directory, I want to read them in a recursive manner. In those cases I use File::Find:

use File::Find;

find sub {
    return if /^[.]/;
    #do stuff with $_ or $File::Find::name
}, $dir;
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
  • The use of `[.]` is very curious---why do you prefer that to `\.`? – C. K. Young Sep 23 '10 at 03:17
  • 1
    @Chris Jester-Young I just like it better. It seems less noisy (see [leaning toothpick syndrome](http://en.wikipedia.org/wiki/Leaning_toothpick_syndrome)). Also, and this is rationalizing, escapes in regex tend to mean "the next character does something special" (e.g. `\x{2e}`), but `\.` means it is just a period. Since most characters don't have special meaning inside a character classes, it makes a nice escape mechanism. – Chas. Owens Sep 23 '10 at 13:37
7

If some of the dotfiles are important,

my @files = grep !/^\.\.?$/, readdir DIR;

will only exclude . and ..

mob
  • 117,087
  • 18
  • 149
  • 283
  • 4
    Well, it might exclude "..\n", which is legal in Unix. Not that I wish that filename on anyone, but people do strange things to see if someone will ignore a file they've snuck into your directory. You can fix that with a \z instead of $ :) – brian d foy Sep 22 '10 at 20:14
5

When I just want the files (as opposed to directories), I use grep with a -f test:

my @files = grep { -f } readdir $dir;
Ether
  • 53,118
  • 13
  • 86
  • 159
  • 6
    This will only work if the directory handle is for the current directory. – C. K. Young Sep 22 '10 at 17:57
  • 1
    @Chris: good point, one would need to chdir first, or construct an absolute path name in the grep with `File::Spec->catfile($dirname, $_)`. – Ether Sep 22 '10 at 21:08
0

Thanks Chris and Ether for your recommendations. I used the following to read a listing of all files (excluded directories), from a directory handle referencing a directory other than my current directory, into an array. The array was always missing one file when not using the absolute path in the grep statement

use File::Slurp; 

print "\nWhich folder do you want to replace text? " ;
chomp (my $input = <>);
if ($input eq "") {
print "\nNo folder entered exiting program!!!\n";
exit 0;
} 

opendir(my $dh, $input) or die "\nUnable to access directory $input!!!\n"; 

my @dir = grep { -f "$input\\$_" } readdir $dh;
Jeff_North
  • 11
  • 2