# Checking if a directory is empty using File

**URL:** <https://www.ruby-forum.com/t/checking-if-a-directory-is-empty-using-file/62583>\
**Category:** Rails\
**Created:** [June 6, 2006, 9:55am UTC](https://www.ruby-forum.com/t/checking-if-a-directory-is-empty-using-file/62583 "2006-06-06T09:55:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [June 6, 2006, 9:55am UTC](https://www.ruby-forum.com/t/checking-if-a-directory-is-empty-using-file/62583/1 "2006-06-06T09:55:34Z")

</div>

Hi,

I have one minor issue. I’m trying to check whether a directory is  
empty  
given some path to the directory.

My code does the following:

if(File.directory?(dirpath)) # dirpath = public/images/1  
if(File.zero?(dirpath))  
FileUtils.rm\_rf(dirpath)  
end  
end

The problem is, while testing this, if I put a file in the directory and  
run  
the above code, it still ends up deleting the entire directory which  
doesn’t  
make sense since File.zero? should not return true. I even tried using  
File.size?(dirpath) but that doesn’t work either.

Any help would be appreciated.

Thanks,

Saureen.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [June 6, 2006, 2:14pm UTC](https://www.ruby-forum.com/t/checking-if-a-directory-is-empty-using-file/62583/2 "2006-06-06T14:14:34Z")

</div>

try something like this instead (in irb)

if File.directory?(dirname)  
begin  
# try to delete the directory  
# Dir.delete raises a SystemCallError if the named directory is not  
empty  
Dir.delete(dirpath)  
rescue SystemCallError =\> e  
# handle the exception here (ie, directory was not deleted because  
it  
was not empty)  
puts e  
end  
else  
puts “#{dirname} is not a directory”  
end

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [June 6, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/checking-if-a-directory-is-empty-using-file/62583/3 "2006-06-06T16:57:21Z")

</div>

Sam D. wrote:

> Hi,
> 
> I have one minor issue. I’m trying to check whether a directory is  
> empty  
> given some path to the directory.
> 
> My code does the following:
> 
> if(File.directory?(dirpath)) # dirpath = public/images/1  
> if(File.zero?(dirpath))  
> FileUtils.rm\_rf(dirpath)  
> end  
> end
> 
> The problem is, while testing this, if I put a file in the directory and  
> run  
> the above code, it still ends up deleting the entire directory which  
> doesn’t  
> make sense since File.zero? should not return true. I even tried using  
> File.size?(dirpath) but that doesn’t work either.
> 
> Any help would be appreciated.

File.zero? will just tell you if you have a zero-length file, which a  
directory is not.

You could say

if Dir.entries(dirpath).size \< 2 # two entries for “.” and “…”

What I’d do, though is:

require ‘pathname’  
if Pathname.new(dirpath).children.size == 0

I’ve already spent enough time struggling with File and Dir; Pathname  
makes things like this a lot easier:-)

–Al Evans

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [June 7, 2006, 11:31am UTC](https://www.ruby-forum.com/t/checking-if-a-directory-is-empty-using-file/62583/4 "2006-06-07T11:31:12Z")

</div>

Why not simply use that expression?

Dir["\*"].empty?

Al Evans schrieb:

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [June 7, 2006, 11:53am UTC](https://www.ruby-forum.com/t/checking-if-a-directory-is-empty-using-file/62583/5 "2006-06-07T11:53:53Z")

</div>

Dir["\*"].empty?
