# Check if directory exists

**URL:** <https://rubytalk.org/t/check-if-directory-exists/43117>\
**Category:** ruby-talk\
**Created:** [18 December 2007 17:15 UTC](https://rubytalk.org/t/check-if-directory-exists/43117 "2007-12-18T17:15:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Florian\_Schaf](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@Florian\_Schaf](https://rubytalk.org/u/Florian_Schaf)\
**Post date:** [18 December 2007 17:15 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/1 "2007-12-18T17:15:27Z")

</div>

hi!  
Can anyone tell me how I can check whether a directory exists or not? I  
didnt find anything via google or in the docs  
thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [18 December 2007 17:30 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/2 "2007-12-18T17:30:02Z")

</div>

What did you Google for, and where did you search in the docs?

> **···**
>
> On Dec 18, 10:15 am, Florian Schaf \<flo.sc...@gmail.com\> wrote:
> 
> > hi!  
> > Can anyone tell me how I can check whether a directory exists or not? I  
> > didnt find anything via google or in the docs  
> > thanks!

---

<div class="post-metadata">

**Author:** ![Karl\_von\_Laudermann](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@Karl\_von\_Laudermann](https://rubytalk.org/u/Karl_von_Laudermann)\
**Post date:** [18 December 2007 17:57 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/3 "2007-12-18T17:57:04Z")

</div>

File#exists? works for directories as well as files. You could then  
use File#directory? to verify that the item is a directory, if it  
exists.

> **···**
>
> On Dec 18, 12:15 pm, Florian Schaf \<flo.sc...@gmail.com\> wrote:
> 
> > hi!  
> > Can anyone tell me how I can check whether a directory exists or not? I  
> > didnt find anything via google or in the docs

---

<div class="post-metadata">

**Author:** ![anti](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@anti](https://rubytalk.org/u/anti)\
**Post date:** [25 January 2008 10:29 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/4 "2008-01-25T10:29:57Z")

</div>

if Dir["C:/ruby"] != nil  
&nbsp;&nbsp;puts 'Yeehoooo !'  
else  
&nbsp;&nbsp;puts 'no such directory'  
end

> **···**
>
> On Dec 18 2007, 9:15 am, Florian Schaf \<flo.sc...@gmail.com\> wrote:
> 
> > hi!  
> > Can anyone tell me how I can check whether a directory exists or not? I  
> > didnt find anything via google or in the docs  
> > thanks!  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Christophe\_Mckeon](https://avatars.discourse-cdn.com/v4/letter/c/ccd318/32.png) [@Christophe\_Mckeon](https://rubytalk.org/u/Christophe_Mckeon)\
**Post date:** [18 December 2007 18:01 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/5 "2007-12-18T18:01:52Z")

</div>

here are two ways:

File.directory? path

test ?d, path

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dan2](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@Dan2](https://rubytalk.org/u/Dan2)\
**Post date:** [25 January 2008 11:27 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/6 "2008-01-25T11:27:53Z")

</div>

File.directory? 'some/dir'

> **···**
>
> On Jan 25, 2008 8:59 PM, anti \<anatoly.volovik@gmail.com\> wrote:
> 
> > On Dec 18 2007, 9:15 am, Florian Schaf \<flo.sc...@gmail.com\> wrote:  
> > \> hi!  
> > \> Can anyone tell me how I can check whether a directory exists or not? I  
> > \> didnt find anything via google or in the docs  
> > \> thanks!  
> > \> --  
> > \> Posted viahttp://www.ruby-forum.com/.
> > 
> > if Dir["C:/ruby"] != nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Yeehoooo !'  
> > else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'no such directory'  
> > end

---

<div class="post-metadata">

**Author:** ![Damphyr](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@Damphyr](https://rubytalk.org/u/Damphyr)\
**Post date:** [25 January 2008 11:37 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/7 "2008-01-25T11:37:00Z")

</div>

anti wrote:

> &nbsp;&nbsp;
> 
> > hi!  
> > Can anyone tell me how I can check whether a directory exists or not? I  
> > didnt find anything via google or in the docs  
> > thanks!  
> > --  
> > Posted viahttp://www.ruby-forum.com/.  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > if Dir["C:/ruby"] != nil  
> > &nbsp;&nbsp;puts 'Yeehoooo !'  
> > else  
> > &nbsp;&nbsp;puts 'no such directory'  
> > end

Yep. And the long version would be

path="/some/path"  
if File.exists?(path) && File.directory?(path)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "yeeha"  
else  
&nbsp;&nbsp;&nbsp;&nbsp;puts "bummer"  
end

😉  
V.-

> **···**
>
> > On Dec 18 2007, 9:15 am, Florian Schaf \<flo.sc...@gmail.com\> wrote:
> 
> --  
> [http://www.braveworld.net/riva](http://www.braveworld.net/riva)

---

<div class="post-metadata">

**Author:** ![Lee\_Jarvis2](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@Lee\_Jarvis2](https://rubytalk.org/u/Lee_Jarvis2)\
**Post date:** [25 January 2008 13:19 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/8 "2008-01-25T13:19:53Z")

</div>

Gavin Kistner wrote:

> **···**
>
> > On Dec 18, 10:15 am, Florian Schaf \<flo.sc...@gmail.com\> wrote:
> > 
> > > hi!  
> > > Can anyone tell me how I can check whether a directory exists or not? I  
> > > didnt find anything via google or in the docs  
> > > thanks!
> > 
> > What did you Google for, and where did you search in the docs?
> 
> Its a shame people had to post solutions afterwards. That would of been  
> interesting.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robby\_Robby](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@Robby\_Robby](https://rubytalk.org/u/Robby_Robby)\
**Post date:** [8 May 2009 19:33 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/9 "2009-05-08T19:33:26Z")

</div>

Lee Jarvis wrote:

> Gavin Kistner wrote:
> 
> > > hi!  
> > > Can anyone tell me how I can check whether a directory exists or not? I  
> > > didnt find anything via google or in the docs  
> > > thanks!
> > 
> > What did you Google for, and where did you search in the docs?
> 
> Its a shame people had to post solutions afterwards. That would of been  
> interesting.

Well im glad they posted solutions, for I googled "ruby directory  
exists" and got the answer from here.

> **···**
>
> > > On Dec 18, 10:15 am, Florian Schaf \<flo.sc...@gmail.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nick\_Larson](https://avatars.discourse-cdn.com/v4/letter/n/6a8cbe/32.png) [@Nick\_Larson](https://rubytalk.org/u/Nick_Larson)\
**Post date:** [1 July 2009 14:31 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/10 "2009-07-01T14:31:53Z")

</div>

Lee Jarvis wrote:

> Its a shame people had to post solutions afterwards. That would of been  
> interesting.

This is why the Ruby and RoR community sucks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tom\_Cloyd2](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@Tom\_Cloyd2](https://rubytalk.org/u/Tom_Cloyd2)\
**Post date:** [2 July 2009 01:20 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/11 "2009-07-02T01:20:53Z")

</div>

Nick Larson wrote:

> Lee Jarvis wrote:  
> &nbsp;&nbsp;
> 
> > Its a shame people had to post solutions afterwards. That would of been interesting.
> 
> This is why the Ruby and RoR community sucks.

What really sucks is list posts like this which fall out of the sky without any internal context.

What this post is about is anyone's guess.

What a time-waster!

t.

> **···**
>
> --
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
> Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist  
> Bellingham, Washington, U.S.A: (360) 920-1226  
> \<\< tc@tomcloyd.com \>\> (email)  
> \<\< TomCloyd.com \>\> (website) \<\< sleightmind.wordpress.com \>\> (mental health weblog)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

<div class="post-metadata">

**Author:** ![Mike\_Bethany1](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@Mike\_Bethany1](https://rubytalk.org/u/Mike_Bethany1)\
**Post date:** [27 September 2010 19:37 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/12 "2010-09-27T19:37:47Z")

</div>

Robby Robby wrote:

> Lee Jarvis wrote:
> 
> > Gavin Kistner wrote:
> > 
> > > > hi!  
> > > > Can anyone tell me how I can check whether a directory exists or not? I  
> > > > didnt find anything via google or in the docs  
> > > > thanks!
> > > 
> > > What did you Google for, and where did you search in the docs?
> > 
> > Its a shame people had to post solutions afterwards. That would of been  
> > interesting.
> 
> Well im glad they posted solutions, for I googled "ruby directory  
> exists" and got the answer from here.

Agreed, I got here from a Google search as well. It amazes me people say  
things like this, that people purporting to espouse the usage of Google  
and searches don't actually have a good understanding how they work.  
They seem to be unable to understand that the Internet is not "right  
now" but is on a time line into the future and future searches will land  
here. Posting responses like "use google" are not only useless but  
demonstrate the person's lack of intellectual ability... but not in a  
good way.

> **···**
>
> > > > On Dec 18, 10:15 am, Florian Schaf \<flo.sc...@gmail.com\> wrote:
> 
> I spoke about this in an article I wrote on how to answer questions, these are how NOT to answer a question. Question: I want to do XYZ using PDQ. Does anyone know what steps I need  
> to take to do this?
> 
> Answer: I do PDQ using XYZ all the time. It’s really hard. You should  
> Google it.  
> Analysis: Not only did they not answer the question they assume you've  
> never heard of Google. They assume you are so sheltered you've never  
> heard of the number one search engine on the face of the planet. They  
> are basically calling you an ignorant, or lazy, fool when in fact they  
> are the ones being intellectually lazy by assuming you DIDN'T search.  
> Worst of all they claim to know how to answer but refuse to do so.
> 
> Answer: This has been asked and answered a million times. Next time use  
> search.  
> Analysis: This guy is closely related to the previous one but scores  
> higher on the ironical incompetence scale. I did use search, that’s how  
> I got here. If you are unfamiliar with how search engines work you  
> should probably Google them.
> 
> I actually figured out myself how to test for the existence of a  
> directory but wanted to see if there was a more "Ruby" way to do it. I  
> now see the .Exists? seem more Ruby-esque but is also more verbose than  
> what I came up with, which is different from the != nil answer, which is  
> actually wrong. Dir[@directory\_name] returns an empty array, not nil. At  
> least in Ruby 1.9.2 it acts this way.
> 
> In following my own advice on giving answers and to paraphrase Bambi's  
> dead mother, "If you don't have something useful to say, STFU." Here's  
> what I came up with to test if a directory does NOT exist; I don't  
> actually use the else part but include it here for completeness.
> 
> if Dir["/non-existant-directory"] == # A non-existant directory  
> returns an empty array, not nil, in Ruby 1.9.2  
> &nbsp;&nbsp;puts "Directory does not exist"  
> else  
> &nbsp;&nbsp;puts "Directory exists"  
> end  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![11150](https://avatars.discourse-cdn.com/v4/letter/1/779978/32.png) [@11150](https://rubytalk.org/u/11150)\
**Post date:** [2 July 2009 06:18 UTC](https://rubytalk.org/t/check-if-directory-exists/43117/13 "2009-07-02T06:18:12Z")

</div>

How to do ?

> **···**
>
> 2009/7/2 Tom Cloyd \<tomcloyd@comcast.net\>
> 
> > Nick Larson wrote:
> > 
> > > Lee Jarvis wrote:
> > > 
> > > > Its a shame people had to post solutions afterwards. That would of been  
> > > > interesting.
> > > 
> > > This is why the Ruby and RoR community sucks.
> > 
> > What really sucks is list posts like this which fall out of the sky without  
> > any internal context.
> > 
> > What this post is about is anyone's guess.
> > 
> > What a time-waster!
> > 
> > t.
> > 
> > --
> > 
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
> > Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist  
> > Bellingham, Washington, U.S.A: (360) 920-1226  
> > \<\< tc@tomcloyd.com \>\> (email)  
> > \<\< TomCloyd.com \>\> (website) \<\< sleightmind.wordpress.com \>\> (mental  
> > health weblog)  
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> --  
> Thanks !
> 
> Kevin Peng
