directory listing without . and ..

Discussion in 'Ruby' started by Rajarshi Chakravarty, Aug 23, 2010.

  1. Hi,
    How can I get an array of all legitimate sub directories in the current
    directory?

    path = "D:/Traffic"
    Dir.chdir(path)

    This gives 2 extra entries "." and ".."
    Dir.open(stationDir).entries.reject{|f| File.file?(f)}

    Please help
    --
    Posted via http://www.ruby-forum.com/.
    Rajarshi Chakravarty, Aug 23, 2010
    #1
    1. Advertising

  2. Rajarshi Chakravarty

    Ryan Davis Guest

    On Aug 22, 2010, at 23:40 , Rajarshi Chakravarty wrote:
    > Hi,
    > How can I get an array of all legitimate sub directories in the =

    current
    > directory?
    >=20
    > path =3D "D:/Traffic"
    > Dir.chdir(path)
    >=20
    > This gives 2 extra entries "." and ".."
    > Dir.open(stationDir).entries.reject{|f| File.file?(f)}


    In all my years of ruby I don't think I've ever used =
    Dir.open(...).entries. I almost _always_ use Dir.glob (aka Dir[...]). I =
    also almost always use the block form for chdir.

    Dir.chdir("/Users/ryan") do
    p Dir[File.join("Work", "*")]
    end

    Outputs:

    > ["Work/Icon\r", "Work/cvs", "Work/git", "Work/mirrors", "Work/misc", =

    "Work/p4", "Work/svn"]

    glob "*" outputs all "visible" files (non-dot files) but it can do a =
    whole lot more too. ri Dir.glob for more details.
    Ryan Davis, Aug 23, 2010
    #2
    1. Advertising

  3. 2010/8/23 Ryan Davis <>:
    > On Aug 22, 2010, at 23:40 , Rajarshi Chakravarty wrote:
    >> Hi,
    >> How can I get an array of all legitimate sub directories in the current
    >> directory?
    >>
    >> path =3D "D:/Traffic"
    >> Dir.chdir(path)
    >>
    >> This gives 2 extra entries "." and ".."
    >> Dir.open(stationDir).entries.reject{|f| File.file?(f)}

    >
    > In all my years of ruby I don't think I've ever used Dir.open(...).entrie=

    s. I almost _always_ use Dir.glob (aka Dir[...]). I also almost always use =
    the block form for chdir.
    >
    > Dir.chdir("/Users/ryan") do
    > =A0p Dir[File.join("Work", "*")]
    > end


    And the chdir isn't even needed here as far as I can see. This should
    do the job:

    dirs =3D Dir[File.join(path, '*')].select {|x| File.directory? x}

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Aug 23, 2010
    #3
  4. Rajarshi Chakravarty, Aug 23, 2010
    #4
  5. On 8/23/10, Ryan Davis <> wrote:
    > On Aug 22, 2010, at 23:40 , Rajarshi Chakravarty wrote:
    >> Hi,
    >> How can I get an array of all legitimate sub directories in the current
    >> directory?
    >>
    >> path = "D:/Traffic"
    >> Dir.chdir(path)
    >>
    >> This gives 2 extra entries "." and ".."
    >> Dir.open(stationDir).entries.reject{|f| File.file?(f)}

    >
    > In all my years of ruby I don't think I've ever used Dir.open(...).entries.
    > I almost _always_ use Dir.glob (aka Dir[...]). I also almost always use the
    > block form for chdir.
    >
    > Dir.chdir("/Users/ryan") do
    > p Dir[File.join("Work", "*")]
    > end
    >
    > Outputs:
    >
    >> ["Work/Icon\r", "Work/cvs", "Work/git", "Work/mirrors", "Work/misc",
    >> "Work/p4", "Work/svn"]

    >
    > glob "*" outputs all "visible" files (non-dot files) but it can do a whole
    > lot more too. ri Dir.glob for more details.


    Omitting all hidden entries may not be what OP wants. This skips not
    only '.' and '..', but also (eg) '.git'. I find this way more robust:

    Dir.entries(whatever)-['.','..']
    Caleb Clausen, Aug 23, 2010
    #5
  6. Rajarshi Chakravarty

    Ryan Davis Guest

    On Aug 23, 2010, at 09:57 , Caleb Clausen wrote:

    >> glob "*" outputs all "visible" files (non-dot files) but it can do a whole
    >> lot more too. ri Dir.glob for more details.

    >
    > Omitting all hidden entries may not be what OP wants. This skips not
    > only '.' and '..', but also (eg) '.git'.


    Yes. I said that.
    Ryan Davis, Aug 23, 2010
    #6
  7. Robert Klemme wrote:
    > dirs = Dir[File.join(path, '*')].select {|x| File.directory? x}


    Use the directory matching power of file globbing patterns:

    dirs = Dir["#{path}/*/"]

    Here, the '/' suffix makes the pattern match directories only!

    Also, note that File.join() always uses '/', even on Windows:

    http://www.ruby-forum.com/topic/50137

    Cheers.
    --
    Posted via http://www.ruby-forum.com/.
    Suraj Kurapati, Aug 23, 2010
    #7
  8. Suraj Kurapati wrote:
    > Robert Klemme wrote:
    >> dirs = Dir[File.join(path, '*')].select {|x| File.directory? x}

    >
    > Use the directory matching power of file globbing patterns:
    >
    > dirs = Dir["#{path}/*/"]
    >
    > Here, the '/' suffix makes the pattern match directories only!


    If you also want to match dot-directories, use more glob power:

    dirs = Dir["#{path}/{*,.[^.]*}/"]

    This will match both normal directories (*/) and dot directories
    (.[^.]*/). But it will fail if you have a directory named '..foo' or
    '........bar', for example.

    Cheers.
    --
    Posted via http://www.ruby-forum.com/.
    Suraj Kurapati, Aug 23, 2010
    #8
    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. JD

    Directory Listing

    JD, Aug 24, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,129
  2. Todd Lu

    Directory listing

    Todd Lu, Aug 31, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    459
    Todd Lu
    Aug 31, 2004
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    directory listing details

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, May 24, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    401
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    May 24, 2005
  4. Tim_Mac
    Replies:
    2
    Views:
    365
    Tim_Mac
    Dec 15, 2005
  5. adam beazley

    Directory and file listing

    adam beazley, Dec 28, 2005, in forum: Ruby
    Replies:
    16
    Views:
    224
    Wilson Bilkovich
    Dec 30, 2005
Loading...

Share This Page