Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 在获取其他目录中特定类型的动态文件的完整路径后,是否检查该文件是否存在?_Perl_File_Filepath_File Extension - Fatal编程技术网

Perl 在获取其他目录中特定类型的动态文件的完整路径后,是否检查该文件是否存在?

Perl 在获取其他目录中特定类型的动态文件的完整路径后,是否检查该文件是否存在?,perl,file,filepath,file-extension,Perl,File,Filepath,File Extension,我有一个目录(在当前脚本执行的上层目录),里面有一些文件。这里我只有1个扩展名为.log的文件类型,但它是*.log base name是动态的,每次脚本运行时都会更改: 。/output/ aaa.txt bbb.txt *.log 在这方面,我想检查文件*.log是否存在,然后用文件名将其完整路径分配给一个变量,以便以后处理。我正在尝试这样做: #!/usr/bin/perl -w use strict; use warnings; use Cwd; my $dir = getcwd;

我有一个目录(在当前脚本执行的上层目录),里面有一些文件。这里我只有1个扩展名为.log的文件类型,但它是*.log base name是动态的,每次脚本运行时都会更改:

。/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的文件之外的所有文件。

您将检查是否有名为
*.log的文件。没有。要查找与
*.log
匹配的文件,请使用
glob
。顺便说一下,当
-e
返回false时,并不意味着该文件不存在。这意味着发生了错误。仅当
$时!{enoint}
也是真的,这是否意味着该文件不存在。您不需要
使用file::Glob
。Perl很好,“它现在起作用了。谢谢大家”,这还不够解释。关于堆栈溢出的页面的主要目的是在其他人遇到类似问题时通知他们解决方案。一旦你自己的问题得到解决,就出手相助是没有用的,也是非常自私的。请在下面写一个答案,描述解决方案,并详细解释您为解决问题所做的工作。这就是我发布结果所需要的。非常感谢:)