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

Reverend Brothers and Sisters,

This humble non programmer seeks your wisdom in a puzzling (for me) matter.

Goal: The bit of code giving me a headache is intended to retrieve the text from all txt files in a directory. It will be used in another script that creates xml files including that text (I am proud to report that my xml script works just fine, processing one file at a time).

What I have so far:

My problem: Why when I put these two together it doesn't work? Is my logic flawed (Oh the shame!)? Here's the offending code:

use locale; use strict; use warnings; if ( $#ARGV != 0 ) { die "Dossier à traiter? Usage : perl openDirFile.pl /users/vot +reNom/Dossier\n"; } my $dossier = "$ARGV[0]"; opendir (DIR, $dossier) or die "Impossible d'ouvrir le dossier ", +"$dossier : ", "$!"; while (my $fichier = readdir (DIR)) { next if $fichier !~ ".\.txt"; $/ = undef; open (FIC, $fichier) or die "Impossible d'ouvrir le fichier ", + "$fichier : ", "$!"; my $texte = <FIC>; print $texte, "\n"; close FIC; } closedir (DIR);

When I run the program I get the error message: Impossible d'ouvrir le fichier MecaLatin.txt : No such file or directory at openDirFile.pl line 15. The aforementioned file is the first one in the directory.

I did try other ways to write the code and lost my sanity in www.perl.org, but I only managed to get more obscure error messages... (sigh!)

Other information that may (or probably will not) be useful: I use Perl 5.8.8 on Mac OS 10.5.8.

Eternal thanks in advance for any insights.

Replies are listed 'Best First'.
Re: Trouble opening files from a directory: "no such file".
by toolic (Bishop) on Aug 09, 2011 at 19:52 UTC
    You need to prepend the directory name to your file name, just like the documentation for readdir advises. Change:
    open (FIC, $fichier) or die "Impossible d'ouvrir le fichier ", "$f +ichier : ", "$!";
    to:
    open (FIC, "$dossier/$fichier") or die "Impossible d'ouvrir le fic +hier ", "$fichier : ", "$!";

      I knew it had to be a silly tiny detail I was missing (I swear I read the documentation on readdir)!

      Thank you toolic. Consider yourself virtually hugged and kissed!

Re: Trouble opening files from a directory: "no such file".
by Gulliver (Monk) on Aug 09, 2011 at 21:59 UTC

    Others have solved your problem but I have a small suggestion. Add a file test in your while loop to make sure you aren't trying to open a directory named 'test.txt' contained in $fichier.

    next unless -T "$dossier/$fichier";
Re: Trouble opening files from a directory: "no such file".
by RichardK (Parson) on Aug 10, 2011 at 12:15 UTC

    There is another way to do this, I find it a lot easier to use glob() to get a list of files.

    my @files = glob("$dir/*.txt");
    @files then contains all the files that match the filename expansion. BTW glob doesn't use regex but expands filenames like the unix shell.

    It's a lot less typing anyway ;)

    Also, you could use File::Slurp to read the files.

    use File::Slurp; my $text = read_file($filename); # or my @lines = read_file($filename);

      Using the normal glob function directly fails if $dir contains directories with whitespace in them. It's better to use the bsd_glob function of File::Glob, or alternatively, to appropriately quote the whitespace in the arguments to glob.