1
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";
            }
        }
    }
}

此代码仅针对目录及其子目录运行。迭代在下一级停止。我想搜索所有空目录。我希望结构是这样的,当我运行代码时,在得到用户的确认后,所有为空的目录都将被删除。提前致谢。

4

2 回答 2

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;
于 2013-07-07T16:50:08.270 回答
1

利用

perl -MFile::Find -e"finddepth(sub{rmdir},'.')"

并看到很好的解释

于 2014-10-15T20:49:33.650 回答