http://qs1969.pair.com?node_id=520791

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi Gr8 People,



I would like to find all the Empty directories from the current directory and delte the same.

Could any one Suggests some Modules to do this task.

Is there a way to find the Empty directory in Perl.

Thanks in advance.

"Keep pouring your ideas"

Replies are listed 'Best First'.
Re: Find Empty directories and removing it
by Tanktalus (Canon) on Jan 04, 2006 at 05:02 UTC

    Just remove them. If they're not empty, rmdir will fail. In this case, you don't care that it fails, ignore the error, and keep going.

    use File::Find; finddepth(sub { rmdir $_ if -d }, $some_path);

    Update: added 'depth' as per merlyn's correction.

      You have to use finddepth, not find, because you need to act on the current directory last. Consider a directory that is empty except for containing two other empty directories.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

Re: Find Empty directories and removing it
by manigandans (Initiate) on Jan 04, 2006 at 05:06 UTC
    Hey Jesuashok,

    You can check the CPAN module File::DirWalk at http://search.cpan.org/~jensl/File-DirWalk-0.2/DirWalk.pm This can help you to find a work-a-round for your requirement.

    regards,
    Mani.
      If you have it installed! Not always possible, especially for small code like this.
Re: Find Empty directories and removing it
by smokemachine (Hermit) on Jan 04, 2006 at 05:27 UTC
    could be this?
    perl -e 'while(<./*>){@a=<$_/*>;rmdir if -d && ! $a[0]}'
Re: Find Empty directories and removing it
by ezekiel (Pilgrim) on Jan 04, 2006 at 05:33 UTC

    Try using the opendir and readdir functions along with a file test. Something like:

    opendir (DIR, ".") or die "Cannot open the current directory: $!"; my @files = grep {$_ ne '.' and $_ ne '..'} readdir DIR; close (DIR) or die "Cannot close the current directory: $!"; foreach my $file (@files) { if (-d $file) { opendir (SUBDIR, "$file") or die "Cannot open the sub director +y: $!; my @subfiles = grep {$_ ne '.' and $_ ne '..'} readdir DIR; closedir(SUBDIR) or die "Cannot close the sub directory: $!"; unless (@subfiles) { unlink("$file"); } } }

    I have not actually tried this to be sure it works. Furthermore, it only deletes empty directories in the current directory. You would need to convert it into a recursive subroutine if you wanted to mine further into the directory tree.

Re: Find Empty directories and removing it
by zentara (Archbishop) on Jan 04, 2006 at 12:23 UTC
Re: Find Empty directories and removing it
by DungeonKeeper (Novice) on Jan 04, 2006 at 13:58 UTC
    An opendir and a grep is not a lot of work. Conversely, the different treatment of the top directory compared with others would make interfacing to File::Find comparatively awkward versus a more direct approach.
    Traverse( "./", 1 ); sub Traverse { # returns 0 for empty else 1 my $dir = shift; my $top = shift; # flag for different treatment of top dir unless( opendir my $dh, $dir ) { warn "$!: $dir\n"; $top or return 1; } for my $file ( grep !/^\.\.?/, readdir $dh ) { my $path = "$dir/$file"; if ( -d $path ) { if ( Traverse( $path, 0 ) ) { $top or return 1; } else { rmdir $path; } } else { $top or return 1; } } closedir $dh; return 0; }