Best way to test if a directory is empty

P

Peter Marks

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.
 
R

Robert Klemme

2007/11/9 said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,483
Latest member
TedDvb6626

Latest Threads

Top