用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 "\n$gAppsDir is not accessible or does not exist.\n";
}
#Ensure Apps directory exists on the test PC.
if ( ! -z $gAppsDir )
{
die "\n$gAppsDir is not accessible or does not exist.\n";
}
上面的这些文件不能正常地告诉我文件夹是空的。谢谢!
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; }
推荐答案
为了清楚起见, p>
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屋!
查看全文