Reputation: 609
I am trying to use -e to check existence of a file, $name is any input specified by user, "_file_"
is something fixed, and * could be anything possible. Currently it is not able to detect the file.
if (-e $name."_file_*.txt)
{
do something;
}
Upvotes: 2
Views: 2255
Reputation: 173662
Why not use glob()
for that?
if (my @files = glob("\Q$name\E_file_*.txt")) {
# do something
}
Upvotes: 8
Reputation: 396
I would suggest you use File::Find module.
use strict;
use warnings;
use File::Find;
# this takes the function a reference and will be executed for each file in the directory.
find({ wanted => \&process, follow => 1 }, '/dir/to/search' );
sub process {
my $filename = $_;
my $filepath = $File::Find::name;
if( $filename=~m/$name\_file\_(.*?)\.txt/ ){
# file exists and do further processing
} else {
# file does not exists
}
}
Upvotes: 1
Reputation: 2589
This is one of the way I could find the existing files with the particular name:
use strict;
use warnings;
use Cwd;
my $name = "Test";
my $curdir = getcwd();
my @txtfiles = glob "$curdir/*.txt";
foreach my $txtfile (@txtfiles)
{
if($txtfile=~m/$name\_file\_(.*?)\.txt/)
{
print "Ok...\n";
}
else { next; }
}
Upvotes: 1