使用 Perl 检测空目录 [英] Detect empty directory with Perl
本文介绍了使用 Perl 检测空目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 perl 中测试文件夹是否为空的简单方法是什么?-s 和 -z 不起作用.
What is an easy way to test if a folder is empty in perl? -s, and -z are not working.
示例:
#Ensure Apps directory exists on the test PC.
if ( ! -s $gAppsDir )
{
die "
$gAppsDir is not accessible or does not exist.
";
}
#Ensure Apps directory exists on the test PC.
if ( ! -z $gAppsDir )
{
die "
$gAppsDir is not accessible or does not exist.
";
}
以上这些,不能正常告诉我文件夹是空的.谢谢!
These above, do not work properly to tell me that the folder is empty. Thanks!
谢谢大家!我最终使用了:
Thanks all! I ended up using:
sub is_folder_empty { my $dirname = shift; opendir(my $dh, $dirname) or die "Not a directory";
return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0; }
推荐答案
为了清楚起见有点冗长,但是:
A little verbose for clarity, but:
sub is_folder_empty {
my $dirname = shift;
opendir(my $dh, $dirname) or die "Not a directory";
return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0;
}
然后你可以这样做:
if (is_folder_empty($your_dir)) {
....
}
这篇关于使用 Perl 检测空目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文