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

Bengt Kleberg <>
Tue Mar 13 16:27:07 CET 2012


Greetings,

An empty directory is 2048 bytes on this machine:
Linux seasc1137 2.6.27.42-0.1-default #1 SMP 2010-01-06 16:07:25 +0100
i686 athlon i386 GNU/Linux

It stays that size when 1 file is added.


bengt

On Tue, 2012-03-13 at 16:18 +0100, Wes James wrote:
> On Tue, Mar 13, 2012 at 12:09 AM, Zabrane Mickael <> 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.
> 
> Another thing to look at.  When I create an empty folder on my mac (I
> tested this with 2 folders and some subfolders), they seem to always
> have a size of 68 bytes when first created (empty).
> 
> I wrote this to work on that idea:
> 
> -module(test).
> 
> -include_lib("kernel/include/file.hrl").
> 
> -define(DARWIN_EMPTY_FOLDER_SIZE,68).
> 
> -export([start/0]).
> 
> start()->
>     is_dir_empty("blah").
> 
> is_dir_empty(Dir) ->
>     {ok, FileInfo} = file:read_file_info(Dir),
>     case FileInfo#file_info.size of
> 	 ?DARWIN_EMPTY_FOLDER_SIZE -> empty;
> 	_ -> notempty
>     end.
> 
> -wes
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list