Best way to test if a directory is empty

I see this question was asked a while back
(http://www.ruby-forum.com/topic/84762), but I’m having trouble
implementing any of the solutions. I have tried:

if Dir[“#{RAILS_ROOT}/documents/”].empty?

if catch(‘empty’){Dir.glob(“#{RAILS_ROOT}/documents/”){throw ‘empty’}}

if Dir.entries(“#{RAILS_ROOT}/documents/”) == []

, but none seem to work for me.

On Nov 9, 1:54 pm, Peter M. [email protected] wrote:

if Dir[“#{RAILS_ROOT}/documents/”].empty?

try this.

if Dir[“#{RAILS_ROOT}/documents/*”].empty?

2007/11/9, Peter M. [email protected]:

I see this question was asked a while back
(Test if Directory is Empty - Ruby - Ruby-Forum), but I’m having trouble
implementing any of the solutions. I have tried:

if Dir[“#{RAILS_ROOT}/documents/”].empty?

You need the star here as well - as you probably know by now.

if catch(‘empty’){Dir.glob(“#{RAILS_ROOT}/documents/”){throw ‘empty’}}

I guess that should rather read

unless catch(‘non_empty’){Dir.glob(“#{RAILS_ROOT}/documents/*”){throw
‘non_empty’}}

Or am I missing something?

if Dir.entries(“#{RAILS_ROOT}/documents/”) == []

(see above)

, but none seem to work for me.

Cheers

robert

pphetra wrote:

try this.

if Dir["#{RAILS_ROOT}/documents/*"].empty?

Ahh, didn’t see the stars. Thanks pphetra :slight_smile: