# File size vs. Directory size problem

**URL:** <https://www.ruby-forum.com/t/file-size-vs-directory-size-problem/178724>\
**Category:** Ruby\
**Created:** [November 10, 2009, 6:47am UTC](https://www.ruby-forum.com/t/file-size-vs-directory-size-problem/178724 "2009-11-10T06:47:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [November 10, 2009, 6:47am UTC](https://www.ruby-forum.com/t/file-size-vs-directory-size-problem/178724/1 "2009-11-10T06:47:50Z")

</div>

Hi all,  
Got a bit of a problem I dont know how to go about.  
Basically my script takes 2 different directories, puts each file/sub  
directory into an array for each, sorts the array, compares them, and  
then spits out what is missing from each directory. Im really happy with  
this as it is my first real ruby script and it works a charm.

Now I was wanting to add to it. Basically I want to take the size of  
each file and see if they are greater than 0, and if it is 0 then output  
the file.

My problem is that a directory is returned as being of size 0. Obviously  
I dont care about the directory size in this case, only the file size.  
How can i write my script to that it ignores the directory and only  
checks if it is a file.

So at the moment my code goes  
Dir.chdir(old)  
old\_array = Array.new  
for i in Dir[’ **/** ’]  
#puts i  
puts i  
puts File.size(i)  
old\_array \<\< i  
end

The puts File.size(i) is there for a semi test, and it shows me that  
directories are returned with size 0.

Any thoughts?

Thanks so much 😃

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [November 10, 2009, 7:02am UTC](https://www.ruby-forum.com/t/file-size-vs-directory-size-problem/178724/2 "2009-11-10T07:02:32Z")

</div>

Maybe something like:

puts File.size(i) unless File.directory?(i)

On Tue, Nov 10, 2009 at 4:47 PM, removed\_email\_address@domain.invalid Tom \<

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [November 10, 2009, 7:04am UTC](https://www.ruby-forum.com/t/file-size-vs-directory-size-problem/178724/3 "2009-11-10T07:04:32Z")

</div>

Sven S. wrote:

> Maybe something like:
> 
> puts File.size(i) unless File.directory?(i)
> 
> On Tue, Nov 10, 2009 at 4:47 PM, removed\_email\_address@domain.invalid Tom \<

I completely missed the directory method haha. Thank you sir.

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [November 11, 2009, 10:43am UTC](https://www.ruby-forum.com/t/file-size-vs-directory-size-problem/178724/4 "2009-11-11T10:43:02Z")

</div>

2009/11/10 Tom T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Sven S. wrote:
> 
> > Maybe something like:
> > 
> > puts File.size(i) unless File.directory?(i)
> > 
> > On Tue, Nov 10, 2009 at 4:47 PM, removed\_email\_address@domain.invalid Tom \<
> 
> I completely missed the directory method haha. Thank you sir.

There is also the not so well known method “test” which can be used for  
this:

old\_array = Dir[“#{old}/\*\*/\*”]  
old\_array.each do |f|  
puts File.size(f) unless test ?d, f  
end

Btw, the original code copied the result of Dir[] into another Array.  
That seems unnecessary additional work. You can directly use the  
result of Dir[].

Kind regards

robert
