0

I want to filter out some files from a directory. I am able to grab the files and their extensions recursively, but now what I want to do is to match the file extension and file name with a predefined array of extensions and file names using wildcard search as we use to do in sql.

my @ignore_exts = qw( .vmdk .iso .7z .bundle .wim .hd .vhd .evtx .manifest .lib .mst );

I want to filter out the files which will have extensions like the above one. e.g. File name is abc.1.149_1041.mst and since the extension .mst is present in @ignore_ext, so I want this to filter out. The extension I am getting is '.1.149_1041.mst'. As in sql I'll do something like select * from <some-table> where extension like '%.mst'. Same thing I want to do in perl.

This is what I am using for grabbing the extension.

my $ext = (fileparse($filepath, '\..*?')) [2];
Biffen
  • 6,249
  • 6
  • 28
  • 36

2 Answers2

2

In order to pull a file extension off a filename this should work:

/^(.*)\.([^.]+)$/
$fileName = $1;
$extension = $2;

This might do the trick for you.

Input: a.b.c.text

$1 will be a.b.c.d

$2 will be text

Basically this will take everything from the start of the line until the last period and group that in the 1st group, and then everything from the last period to the end of the line as group 2

You can see a sample here: http://regex101.com/r/vX3dK1

As for checking whether the extension exists in the array read here: (How can I check if a Perl array contains a particular value?)

if (grep (/^$extension/, @array)) {
  print "Extension Found\n"; 
} 
Community
  • 1
  • 1
Jeef
  • 26,861
  • 21
  • 78
  • 156
0

Just turn your list of extensions into a regular expression, and then test against the $filepath.

my @ignore_exts = qw( .vmdk .iso .7z .bundle .wim .hd .vhd .evtx .manifest .lib .mst );
my $ignore_exts_re = '(' . join('|', map quotemeta, @ignore_exts) . ')$';

And then later to compare

if ($filepath =~ $ignore_exts_re) {
    print "Ignore $filepath because it ends in $1\n";
    next;
Miller
  • 34,962
  • 4
  • 39
  • 60