views:

132

answers:

5

I'm using this code to get a list of all the files in a specific directory:

opendir DIR, $dir or die "cannot open dir $dir: $!";
my @files= readdir DIR;
closedir DIR;

How can I modify this code or append something to it so that it only looks for text files and only loads the array with the prefix of the filename?

Example directory contents:

.
..
923847.txt
98398523.txt
198.txt
deisi.jpg
oisoifs.gif
lksdjl.exe

Example array contents:

files[0]=923847 
files[1]=98398523
files[2]=198
+2  A: 
my @files = glob "$dir/*.txt";
for (0..$#files){
  $files[$_] =~ s/\.txt$//;
}
Wooble
any idea how to regex the directory out too? my output is /dir/dir/dir/923847... how can i just get the 923847?
CheeseConQueso
glob adds extra work here. See http://stackoverflow.com/questions/1506801/what-reasons-are-there-to-prefer-glob-over-readdir-or-vice-versa-in-perl
brian d foy
+4  A: 

it is enough to change one line:

my @files= map{s/\.[^.]+$//;$_}grep {/\.txt$/} readdir DIR;
catwalk
+2  A: 

To get just the ".txt" files, you can use a file test operator (-f : regular file) and a regex.

my @files = grep { -f && /\.txt$/ } readdir $dir;

Otherwise, you can look for just text files, using perl's -T (ascii-text file test operator)

my @files = grep { -T } readdir $dir;
Robert P
-T is for testing whether you have a "textfile"
reinierpost
Good point; from the perldoc page on the operator (http://perldoc.perl.org/5.8.8/perlfunc.html), "-T File is an ASCII text file (heuristic guess)." if he's looking for ".txt" files, this will do exactly what he asks without guessing.
Robert P
+2  A: 

If you can use the new features of Perl 5.10, this is how I would write it.

use strict;
use warnings;
use 5.10.1;
use autodie; # don't need to check the output of opendir now

my $dir = ".";

{
  opendir my($dirhandle), $dir;
  for( readdir $dirhandle ){ # sets $_
    when(-d $_ ){ next } # skip directories
    when(/^[.]/){ next } # skip dot-files

    when(/(.+)[.]txt$/){ say "text file: ", $1 }
    default{
      say "other file: ", $_;
    }
  }
  # $dirhandle is automatically closed here
}

Or if you have very large directories, you could use a while loop.

{
  opendir my($dirhandle), $dir;
  while( my $elem = readdir $dirhandle ){
    given( $elem ){ # sets $_
      when(-d $_ ){ next } # skip directories
      when(/^[.]/){ next } # skip dot-files

      when(/(.+)[.]txt$/){ say "text file: ", $1 }
      default{
        say "other file: ", $_;
      }
    }
  }
}
Brad Gilbert
+1  A: 

Just use this:

my @files = map {-f && s{\.txt\z}{} ? $_ : ()} readdir DIR;
Hynek -Pichi- Vychodil