Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Find Empty directories and removing it

by DungeonKeeper (Novice)
on Jan 04, 2006 at 13:58 UTC ( #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?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://520903]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-05-19 23:39 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (72 votes). Check out past polls.