我有一个目录(在当前脚本执行的上层目录中),里面有一些文件。这里我只有一个带.log扩展名的文件类型,但它的* .log基本名称是动态的,每次脚本运行时都会更改:
../output/
aaa.txt bbb.txt *.log
在此,我想检查文件* .log是否存在,然后将其带文件名的完整路径分配给变量以便稍后进行处理。我正在尝试这个:
#!/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");
}
但我总是得到$ filepath不存在。有什么错吗?
你可以使用perl glob。 将您的脚本更改为:
#!/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");
}
但是它会忽略除* .log之外的所有文件。