Check if directory exists

Discussion in 'Ruby' started by Florian Schaf, Dec 18, 2007.

  1. 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/.
     
    Florian Schaf, Dec 18, 2007
    #1
    1. Advertising

  2. Florian Schaf

    Phrogz Guest

    On Dec 18, 10:15 am, Florian Schaf <> 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?
     
    Phrogz, Dec 18, 2007
    #2
    1. Advertising

  3. On Dec 18, 12:15 pm, Florian Schaf <> 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


    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.
     
    Karl von Laudermann, Dec 18, 2007
    #3
  4. Christophe Mckeon, Dec 18, 2007
    #4
  5. Florian Schaf

    anti Guest

    On Dec 18 2007, 9:15 am, Florian Schaf <> 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
    puts 'Yeehoooo !'
    else
    puts 'no such directory'
    end
     
    anti, Jan 25, 2008
    #5
  6. Florian Schaf

    Dan Guest

    [Note: parts of this message were removed to make it a legal post.]

    File.directory? 'some/dir'



    On Jan 25, 2008 8:59 PM, anti <> wrote:

    > On Dec 18 2007, 9:15 am, Florian Schaf <> 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
    > puts 'Yeehoooo !'
    > else
    > puts 'no such directory'
    > end
    >
    >
     
    Dan, Jan 25, 2008
    #6
  7. anti wrote:
    > On Dec 18 2007, 9:15 am, Florian Schaf <> 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
    > puts 'Yeehoooo !'
    > else
    > puts 'no such directory'
    > end
    >
    >
    >

    Yep. And the long version would be

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

    ;)
    V.-

    --
    http://www.braveworld.net/riva
     
    Vassilis Rizopoulos, Jan 25, 2008
    #7
  8. Florian Schaf

    Lee Jarvis Guest

    Gavin Kistner wrote:
    > On Dec 18, 10:15 am, Florian Schaf <> 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/.
     
    Lee Jarvis, Jan 25, 2008
    #8
  9. Florian Schaf

    Robby Robby Guest

    Lee Jarvis wrote:
    > Gavin Kistner wrote:
    >> On Dec 18, 10:15 am, Florian Schaf <> 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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Robby Robby, May 8, 2009
    #9
  10. Florian Schaf

    Nick Larson Guest

    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/.
     
    Nick Larson, Jul 1, 2009
    #10
  11. Florian Schaf

    Tom Cloyd Guest

    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
    << >> (email)
    << TomCloyd.com >> (website)
    << sleightmind.wordpress.com >> (mental health weblog)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    Tom Cloyd, Jul 2, 2009
    #11
  12. Florian Schaf

    µËÅô Guest

    [Note: parts of this message were removed to make it a legal post.]

    2009/7/2 Tom Cloyd <>

    > 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
    > << >> (email)
    > << TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental
    > health weblog)
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    >
    >

    How to do ?


    --
    Thanks !


    Kevin Peng
     
    µËÅô, Jul 2, 2009
    #12
  13. Florian Schaf

    Mike Bethany Guest

    Robby Robby wrote:
    > Lee Jarvis wrote:
    >> Gavin Kistner wrote:
    >>> On Dec 18, 10:15 am, Florian Schaf <> 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.

    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
    puts "Directory does not exist"
    else
    puts "Directory exists"
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Mike Bethany, Sep 27, 2010
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pgar
    Replies:
    1
    Views:
    4,934
    =?Utf-8?B?U2FudGhhbmEgR29wYWxhbg==?=
    Nov 15, 2004
  2. Joris De Groote

    Check if a directory exists in a network drive?

    Joris De Groote, Apr 11, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    3,637
    bruce barker \(sqlwork.com\)
    Apr 11, 2006
  3. calca77

    check if a directory exists

    calca77, Jan 9, 2004, in forum: C++
    Replies:
    3
    Views:
    13,576
    Christoph Rabel
    Jan 9, 2004
  4. Jason Stacy
    Replies:
    2
    Views:
    4,780
    Roedy Green
    Apr 27, 2008
  5. Ulf Meinhardt
    Replies:
    8
    Views:
    6,230
Loading...

Share This Page