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

Wes James <>
Tue Mar 13 16:18:01 CET 2012


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



More information about the erlang-questions mailing list