[erlang-questions] Best way to check if a given directory is empty (or not)?

Taavi Talvik taavi@REDACTED
Tue Mar 13 17:01:03 CET 2012


On Mar 13, 2012, at 5:18 PM, Wes James wrote:

> On Tue, Mar 13, 2012 at 12:09 AM, Zabrane Mickael <zabrane3@REDACTED> wrote:
>> Hi guys,
>> 
>> I'd like to know if there's a good and portable method to check if a directory is empty (or not)?
>> In my case, the directory can contains huge number (more than 150K in average) of files if it is not empty.


On unix systems link_count=2 means, that directory is empty:

3> rr("/usr/local/lib/erlang/lib/kernel-2.14.1/include/file.hrl").
[file_descriptor,file_info]
4> file:read_file_info(".").                                      
{ok,#file_info{size = 170,type = directory,
               access = read_write,
               atime = {{2012,3,13},{17,57,1}},
               mtime = {{2007,9,30},{14,7,32}},
               ctime = {{2012,3,11},{3,6,39}},
               mode = 16877,links = 5,major_device = 234881026,
               minor_device = 0,inode = 43374422,uid = 501,gid = 501}}

2> file:read_file_info("empty").                                  
{ok,#file_info{size = 68,type = directory,
               access = read_write,
               atime = {{2012,3,13},{17,59,44}},
               mtime = {{2012,3,13},{17,59,44}},
               ctime = {{2012,3,13},{17,59,44}},
               mode = 16877,links = 2,major_device = 234881026,
               minor_device = 0,inode = 43401684,uid = 501,gid = 501}}

best regards,
taavi
--
"We should forget about small efficiencies, about 97 percent of the time. 
Premature optimization is the root of all evil." Donald Knuth

Taavi Talvik
taavi@REDACTED
+372 5656 9996




More information about the erlang-questions mailing list