Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Find Empty directories and removing it

by ezekiel (Pilgrim)
on Jan 04, 2006 at 05:33 UTC ( #520799=note: print w/replies, xml ) Need Help??

in reply to Find Empty directories and removing it

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.

Log In?

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

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

    Results (84 votes). Check out past polls.