in reply to list of fles in a directory, but use wildcards like *

my @files = glob 'onlythese*.txt';

After Compline,
Zaxo

  • Comment on Re: list of fles in a directory, but use wildcards like *

Replies are listed 'Best First'.
Re: Re: list of fles in a directory, but use wildcards like *
by LeeC79 (Acolyte) on Sep 04, 2003 at 16:44 UTC
    When I was using,
    opendir DIR, $dir; my @file = grep { $_ ne '.' && $_ ne '..' } readdir DIR; closedir DIR;
    to list all files in the directory, opening those files with,
    foreach $file (@file){ open(MYFILE, $dir.$file[$i]) or die qq(Cannot open $file[$i]\n); ....some other stuff
    would work. But now using the glob, it's dying when open the files in the files list.
        foreach $file (@file){ open(MYFILE, $dir.$file[$i]) or die qq(Cannot open $file[$i]\n); ...
        Well, in addition to the previous comment about the directory path (which is correct), this snippet would not do what you want because you aren't setting $i; you are using the scalar $file as an iterator variable, which is being set to successive elements of array @file, but then in the loop you are always using the "$i'th" element of @file, and you don't show any indication that this (totally unnecessary) array index counter is being set or incremented.

        On top of that, your error report is misleading: you try to open a file called  $dir.$file[$i] and when that fails, you only report trying to open  $file[$i], so you would never know when the problem is due to the content of $dir.