while (defined($name = readdir $dh)) {
next unless -d "$root/$name";
$dir=$root.$name."/";
if(is_folder_empty($dir)){
print"$dir is Empty\n";
print"Do you want to delete it? 1-Yes 2-No\n";
$aa=<STDIN>;
chomp $aa;
if($aa==1){
$deleteregdir = "$dir";
rmdir $deleteregdir;
print"Directory has been deleted!!\n";
}
}
else
{
print"$dir is NOT Empty\n";
$dir2=$dir;
opendir my $dh2, $dir2;
while (defined($name2 = readdir $dh2)) {
next unless -d "$dir2/$name2";
$dd=$dir2.$name2."/";
#print"$dd\n";
if(is_folder_empty($dd)){
print"$dd is Empty\n";
print"Do you want to delete it? 1-Yes 2-No\n";
$aa=<STDIN>;
chomp $aa;
if($aa==1){
$deleteregdir = "$dir";
rmdir $deleteregdir;
print"Directory has been deleted!!\n";
}
}
else
{
print"$dd is NOT Empty\n";
}
}
}
}
此代码仅针对目录及其子目录运行。迭代在下一级停止。我想搜索所有空目录。我希望结构是这样的,当我运行代码时,所有空目录都会在收到用户确认后删除。 提前谢谢。
答案 0 :(得分:2)
如果你愿意使用模块,File::Util会让这种事情变得非常简单。
use strict;
use warnings;
use File::Util;
my $path = '...';
my $fu = File::Util->new();
my @all_dirs = $fu->list_dir($path, '--recurse', '--dirs-only');
my @empty_dirs = grep { not $fu->list_dir($_) } @all_dirs;
答案 1 :(得分:1)