# Testing for Directories and Files

**URL:** <https://www.ruby-forum.com/t/testing-for-directories-and-files/132797>\
**Category:** Ruby\
**Created:** [March 24, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/testing-for-directories-and-files/132797 "2008-03-24T20:17:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jstotz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jstotz](https://www.ruby-forum.com/u/jstotz)\
**Post date:** [March 24, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/testing-for-directories-and-files/132797/1 "2008-03-24T20:17:03Z")

</div>

Hello. I have a question regarding Files and Directories. I have a  
class method that opens a directory and reads the contents. What is the  
most efficient way to test whether an entry is a directory or a file?

Also, the purpose of this program is to read through a tree directory  
structure and turn the directory names into database entries. Any  
suggestions on how to make a complete spider that utilizes Ruby’s  
specialties?

Thanks!

---

<div class="post-metadata">

**Author:** ![jstotz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jstotz](https://www.ruby-forum.com/u/jstotz)\
**Post date:** [March 24, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/testing-for-directories-and-files/132797/2 "2008-03-24T21:15:31Z")

</div>

On 24.03.2008 20:16, Jay Sanders wrote:

> Hello. I have a question regarding Files and Directories. I have a  
> class method that opens a directory and reads the contents. What is the  
> most efficient way to test whether an entry is a directory or a file?

File.directory? path  
File.file? path

> Also, the purpose of this program is to read through a tree directory  
> structure and turn the directory names into database entries. Any  
> suggestions on how to make a complete spider that utilizes Ruby’s  
> specialties?

Look at Find.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jstotz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jstotz](https://www.ruby-forum.com/u/jstotz)\
**Post date:** [March 24, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/testing-for-directories-and-files/132797/3 "2008-03-24T21:48:03Z")

</div>

> > Also, the purpose of this program is to read through a tree directory  
> > structure and turn the directory names into database entries. Any  
> > suggestions on how to make a complete spider that utilizes Ruby’s  
> > specialties?

If you are on a \*ix box you can create your dir/file database getting  
the friendly output of ‘du -a /mypath \> somefile’ …size are also  
included.

-r.
