Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Find Empty directories and removing it

by DungeonKeeper (Novice)
on Jan 04, 2006 at 13:58 UTC ( [id://520903]=note: print w/replies, xml ) Need Help??


in reply to Find Empty directories and removing it

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://520903]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-29 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found