Zbyslaw Railerski
Zbyslaw Railerski

Reputation: 61

Checking if a file exists based on a wildcard in the file name

I have a set of partial filenames, that I need to check against subsequent data delivery packages. For example, some of the entries in my "blue print" set are:

<NAME>
FILE_ONE
FILE_TWO
FILE_THREE

The actual delivery will contain following files:

<NAME>_<TIMESTAMP>_<SEQUENCE>.<EXTENSION>
FILE_ONE_20180712104010_001.CSV
FILE_TWO_20180712112510_001.CSV
FILE_THREE_20180712112920_001.CSV

Now I would like to read my blue print list in a loop and check whether all files arrived. I do a check via the name + I would like to concatenate any 14 digits + I would like to concatenate the sequence number coming as a parameter + I would like to concatenate the extension.

My code looks like this:

my $bp="blueprint.txt"; #list of partial file names I would like to look for

open my $handle, '<', $bp;
chomp(my @files = <$handle>);
close $handle;

foreach (@files) {
    if(! -f "$_" + "_/\d{14}/_" + $ARGV[0] + ".CSV")
    {
      print "$_ does not exist\n";
    }
}

It throws following errors:

Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE

Please help me with the if statement.

Upvotes: 0

Views: 629

Answers (1)

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

Reputation: 40718

You could also try using readdir to collect all filenames in the current directory. For example:

my $seq_num = $ARGV[0];
my $dir = '.';
my @matches;

opendir(my $dh, $dir) or die "Can't opendir $dir: $!";
while (my $name = readdir $dh) {
    for my $bp ( @files ) {
        if ( $name =~ /^\Q$bp\E_\d{14}_\Q$seq_num\E\.CSV/ ) {
            push @matches, $bp;
            last;
        }
    }
}
closedir $dh;

Upvotes: 1

Related Questions