Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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 exploiting the Monastery: (3)
As of 2024-04-25 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found