Search code examples
perlgrepreaddir

Filter filenames by pattern


I need to search for files in a directory that begin with a particular pattern, say "abc". I also need to eliminate all the files in the result that end with ".xh". I am not sure how to go about doing it in Perl.

I have something like this:

opendir(MYDIR, $newpath);
my @files = grep(/abc\*.*/,readdir(MYDIR)); # DOES NOT WORK

I also need to eliminate all files from result that end with ".xh"

Thanks, Bi


Solution

  • try

    @files = grep {!/\.xh$/} <$MYDIR/abc*>;
    

    where MYDIR is a string containing the path of your directory.