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

Wes James <>
Tue Mar 13 16:33:33 CET 2012


Per Bengt's experience.  What happens if you add a file?  Does the size change?

-wes

On Tue, Mar 13, 2012 at 9:29 AM, Zabrane Mickael <> wrote:
> Awesome Wes.
>
> On my OSX 10.7.3:  68 bytes
> On Linux 32/64 bits Fedora: 4096 bytes
>
> On Mar 13, 2012, at 4:18 PM, 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
>
> Regards,
> Zabrane
>



More information about the erlang-questions mailing list