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

Multi tool use
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 existn";
}
}
It throws following errors:
Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE
Please help me with the if
statement.
regex file perl
add a comment |
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 existn";
}
}
It throws following errors:
Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE
Please help me with the if
statement.
regex file perl
7
+ is not a string concatenation operator in Perl.
– toolic
Jan 2 at 15:59
5
glob
is an alternative to regex matching that you can use with filenames
– mob
Jan 2 at 15:59
2
Where's the rest of the error message (in particular, the line number)?
– melpomene
Jan 2 at 16:01
1
I don't think that error message was generated by the code you've shown us. There's no match operator (m/.../
) in that code. The only thing that looks like one (/d{14}/
) is actually just a string.
– Dave Cross
Jan 2 at 16:04
add a comment |
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 existn";
}
}
It throws following errors:
Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE
Please help me with the if
statement.
regex file perl
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 existn";
}
}
It throws following errors:
Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE
Please help me with the if
statement.
regex file perl
regex file perl
edited Jan 2 at 16:00
melpomene
61.7k54994
61.7k54994
asked Jan 2 at 15:55
Zbyslaw RailerskiZbyslaw Railerski
2114
2114
7
+ is not a string concatenation operator in Perl.
– toolic
Jan 2 at 15:59
5
glob
is an alternative to regex matching that you can use with filenames
– mob
Jan 2 at 15:59
2
Where's the rest of the error message (in particular, the line number)?
– melpomene
Jan 2 at 16:01
1
I don't think that error message was generated by the code you've shown us. There's no match operator (m/.../
) in that code. The only thing that looks like one (/d{14}/
) is actually just a string.
– Dave Cross
Jan 2 at 16:04
add a comment |
7
+ is not a string concatenation operator in Perl.
– toolic
Jan 2 at 15:59
5
glob
is an alternative to regex matching that you can use with filenames
– mob
Jan 2 at 15:59
2
Where's the rest of the error message (in particular, the line number)?
– melpomene
Jan 2 at 16:01
1
I don't think that error message was generated by the code you've shown us. There's no match operator (m/.../
) in that code. The only thing that looks like one (/d{14}/
) is actually just a string.
– Dave Cross
Jan 2 at 16:04
7
7
+ is not a string concatenation operator in Perl.
– toolic
Jan 2 at 15:59
+ is not a string concatenation operator in Perl.
– toolic
Jan 2 at 15:59
5
5
glob
is an alternative to regex matching that you can use with filenames– mob
Jan 2 at 15:59
glob
is an alternative to regex matching that you can use with filenames– mob
Jan 2 at 15:59
2
2
Where's the rest of the error message (in particular, the line number)?
– melpomene
Jan 2 at 16:01
Where's the rest of the error message (in particular, the line number)?
– melpomene
Jan 2 at 16:01
1
1
I don't think that error message was generated by the code you've shown us. There's no match operator (
m/.../
) in that code. The only thing that looks like one (/d{14}/
) is actually just a string.– Dave Cross
Jan 2 at 16:04
I don't think that error message was generated by the code you've shown us. There's no match operator (
m/.../
) in that code. The only thing that looks like one (/d{14}/
) is actually just a string.– Dave Cross
Jan 2 at 16:04
add a comment |
1 Answer
1
active
oldest
votes
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$bpE_d{14}_Q$seq_numE.CSV/ ) {
push @matches, $bp;
last;
}
}
}
closedir $dh;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009339%2fchecking-if-a-file-exists-based-on-a-wildcard-in-the-file-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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$bpE_d{14}_Q$seq_numE.CSV/ ) {
push @matches, $bp;
last;
}
}
}
closedir $dh;
add a comment |
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$bpE_d{14}_Q$seq_numE.CSV/ ) {
push @matches, $bp;
last;
}
}
}
closedir $dh;
add a comment |
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$bpE_d{14}_Q$seq_numE.CSV/ ) {
push @matches, $bp;
last;
}
}
}
closedir $dh;
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$bpE_d{14}_Q$seq_numE.CSV/ ) {
push @matches, $bp;
last;
}
}
}
closedir $dh;
answered Jan 3 at 11:56


Håkon HæglandHåkon Hægland
16.1k124393
16.1k124393
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009339%2fchecking-if-a-file-exists-based-on-a-wildcard-in-the-file-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
oNYv12 9odp A wdEbIEua9hEc
7
+ is not a string concatenation operator in Perl.
– toolic
Jan 2 at 15:59
5
glob
is an alternative to regex matching that you can use with filenames– mob
Jan 2 at 15:59
2
Where's the rest of the error message (in particular, the line number)?
– melpomene
Jan 2 at 16:01
1
I don't think that error message was generated by the code you've shown us. There's no match operator (
m/.../
) in that code. The only thing that looks like one (/d{14}/
) is actually just a string.– Dave Cross
Jan 2 at 16:04