Reputation: 73
I have a directory(at upper directory of current script execution) with some files inside. Here i have only 1 file type with .log extension, but it's *.log base name is dynamic and will be changed every time script running:
../ouput/
aaa.txt bbb.txt *.log
On this,i want to check the file *.log exist or not, then assign it's full path with file name to a variable for process later. I'm trying with this:
#!/usr/bin/perl -w
use strict;
use warnings;
use Cwd;
my $dir = getcwd; # Get current directory path into $dir
my $filepath = $dir . '..\\output\\*.log';
# ..\\output\\ to get Upper location and output folder
# assign any "*" base name file with type ".log" to $filepath
if(-e $filepath ) {
print("File $filepath exist \n");
}
else
{
print("File $filepath does not exist \n");
}
But i always get $filepath does not exist. Is there any mistake?
Upvotes: 0
Views: 194
Reputation: 2543
You could use perl glob.
Change your script to this:
#!/usr/bin/perl -w
use strict;
use warnings;
use Cwd;
my $dir = getcwd; # Get current directory path into $dir
my $filepath = glob($dir . '/../output/*.log');
# ..\\output\\ to get Upper location and output folder
# assign any "*" base name file with type ".log" to $filepath
if(-e $filepath ) {
print("File $filepath exist \n");
}
else
{
print("File $filepath does not exist \n");
}
It will however ignore all but the first files that match *.log.
Upvotes: 1