# Best way to test if a directory is empty

**URL:** <https://www.ruby-forum.com/t/best-way-to-test-if-a-directory-is-empty/119689>\
**Category:** Ruby\
**Created:** [November 9, 2007, 7:54am UTC](https://www.ruby-forum.com/t/best-way-to-test-if-a-directory-is-empty/119689 "2007-11-09T07:54:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [November 9, 2007, 7:54am UTC](https://www.ruby-forum.com/t/best-way-to-test-if-a-directory-is-empty/119689/1 "2007-11-09T07:54:07Z")

</div>

I see this question was asked a while back  
([http://www.ruby-forum.com/topic/84762](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.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [November 9, 2007, 10:35am UTC](https://www.ruby-forum.com/t/best-way-to-test-if-a-directory-is-empty/119689/2 "2007-11-09T10:35:47Z")

</div>

On Nov 9, 1:54 pm, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if Dir[“#{RAILS\_ROOT}/documents/”].empty?

try this.

if Dir[“#{RAILS\_ROOT}/documents/\*”].empty?

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [November 9, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/best-way-to-test-if-a-directory-is-empty/119689/3 "2007-11-09T18:46:36Z")

</div>

2007/11/9, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I see this question was asked a while back  
> ([Test if Directory is Empty - Ruby - Ruby-Forum](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

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [November 9, 2007, 11:57am UTC](https://www.ruby-forum.com/t/best-way-to-test-if-a-directory-is-empty/119689/4 "2007-11-09T11:57:54Z")

</div>

pphetra wrote:

> try this.
> 
> if Dir["#{RAILS\_ROOT}/documents/\*"].empty?

Ahh, didn’t see the stars. Thanks pphetra 🙂
