in reply to Re^2: Perl List files in directory
in thread Perl List files in directory

I'd suggest a small tweak to that:

@files = grep {-f "$dir/$_" && /\.xml$/i} readdir DIR;

Because otherwise the regular expression would have matched a file named "fooxml.bar" as well. Also, this now matches the extension case-insensitively, just in case.

Replies are listed 'Best First'.
Re^4: Perl List files in directory
by Anonymous Monk on Apr 24, 2014 at 14:22 UTC

    Hi,

    I have tried your tweak, but now i'm getting an error:

    Error = Illegal seak at ./script.pl line 28

    $directory = "/opt/tmp/"; opendir(DIR, $directory) or die "\nCannot open dir : $!\n"; @files = grep {-f "$directory/$_" && /\.xml$/i} readdir or die "Error += $!"; closedir(DIR);

    Can someone help me?

      The ... or die does not make sense when used in list context.

      The cause you get your error is most likely because the readdir does not return any files. Maybe you meant

      readdir DIR;

      ... instead of a bare readdir?

        problem solved:

        @files = grep (/\.xml$/, readdir(DIR));

        Thanks for the help