Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Find Empty directories and removing it

by jesuashok (Curate)
on Jan 04, 2006 at 04:36 UTC ( #520791=perlquestion: print w/ replies, xml ) Need Help??
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"

Comment on Find Empty directories and removing it
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.
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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://520791]
Approved by spiritway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-12-27 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls