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

Masklinn <>
Tue Mar 13 17:10:39 CET 2012

On 2012-03-13, at 16:57 , Miles Fidelman wrote:
> um...
> given an empty directory:
> 7> file:list_dir("/Users/mfidelman/empty") == {ok,[]}.
> true
> given a non-empty directory:
> 8> file:list_dir("/Users/mfidelman") == {ok,[]}.
> false

Quoting the original message:

> The problem with them is they both return a list of filenames.

file:list_dir is fine when the directory has a few hundred files tops,
but OP quotes file counts beyond 150k, which which yield a few megabytes
of data moving around before discarding everything in this final match.

Not to mention filesystem (in)efficiencies which may make fetching 150k
names *very* slow (on HFS+ — admittedly one hell of a POS — merely
calling `ls` on a directory with ~60k files and redirecting the result
to /dev/null takes a good 200ms)

More information about the erlang-questions mailing list