Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Quickly checking if a directory is empty

by alexm (Chaplain)
on Aug 18, 2009 at 20:55 UTC ( [id://789592]=note: print w/replies, xml ) Need Help??


in reply to Quickly checking if a directory is empty

Using File::Slurp::read_dir() as in:

use File::Slurp; if ( read_dir('/path/to/dir') == 0 ) { print "is empty!\n"; }

Update: using File::Slurp or Path::Class will have bad performance on a directory with a large number of entries. However, File::Find::Rule::DirectoryEmpty is addressing the problem the right way, as mentioned by Fletch, by looking for any other entry aside from "." and ".." (which means 3 readdir calls at most whatever the number of entries).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2025-07-11 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.