【发布时间】:2011-05-28 11:12:46
【问题描述】:
在 perl 中测试文件夹是否为空的简单方法是什么? -s 和 -z 不起作用。
例子:
#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";
}
上面这些,不能正常告诉我文件夹是空的。谢谢!
谢谢大家!我最终使用了:
sub is_folder_empty { my $dirname = shift; opendir(my $dh, $dirname) or die "Not a directory";
return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0; }
【问题讨论】: