Using Dir.glob to grab only Directories

Discussion in 'Ruby' started by Mario Gr, Jul 3, 2009.

  1. Mario Gr

    Mario Gr Guest

    Hi,

    I was wondering if there were a way to use Dir.glob to grab only
    directories. After searching google for a bit I found ways to pull only
    file listings, but couldn't find one for just directories.

    Currently, I'm using FileTest.directory?(dir) before running any
    operations to test for a directory.
    --
    Posted via http://www.ruby-forum.com/.
    Mario Gr, Jul 3, 2009
    #1
    1. Advertising

  2. Mario Gr

    Andrea Fazzi Guest

    Mario Gr wrote:
    > Hi,
    >
    > I was wondering if there were a way to use Dir.glob to grab only
    > directories. After searching google for a bit I found ways to pull only
    > file listings, but couldn't find one for just directories.
    >
    > Currently, I'm using FileTest.directory?(dir) before running any
    > operations to test for a directory.


    # Non-recursive
    Dir.glob('*').select { |fn| File.directory?(fn) }

    # Recursive
    Dir.glob('**/*').select { |fn| File.directory?(fn) }


    --
    Posted via http://www.ruby-forum.com/.
    Andrea Fazzi, Jul 3, 2009
    #2
    1. Advertising

  3. On 03.07.2009 20:43, Andrea Fazzi wrote:
    > Mario Gr wrote:
    >> Hi,
    >>
    >> I was wondering if there were a way to use Dir.glob to grab only
    >> directories. After searching google for a bit I found ways to pull only
    >> file listings, but couldn't find one for just directories.
    >>
    >> Currently, I'm using FileTest.directory?(dir) before running any
    >> operations to test for a directory.

    >
    > # Non-recursive
    > Dir.glob('*').select { |fn| File.directory?(fn) }
    >
    > # Recursive
    > Dir.glob('**/*').select { |fn| File.directory?(fn) }


    For deep hierarchies it may be more efficient to use Find because you
    avoid creating a large Array with lots of file names most of which you
    do not want.

    # untested
    require 'find'
    DOTS = %w{. ..}

    Find.find base_dir do |f|
    next if DOTS.include?(File.basename(f)) || !test(?d, f)
    puts "A dir which is not . or ..: #{f}"
    end

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jul 3, 2009
    #3
  4. Mario Gr

    Intransition Guest

    On Jul 3, 2:09=A0pm, Mario Gr <> wrote:
    > Hi,
    >
    > I was wondering if there were a way to use Dir.glob to grab only
    > directories. =A0After searching google for a bit I found ways to pull onl=

    y
    > file listings, but couldn't find one for just directories.
    >
    > Currently, I'm using FileTest.directory?(dir) before running any
    > operations to test for a directory.


    Trailing '/':

    Dir.glob('*/').select { |fn| File.directory?(fn) }
    Dir.glob('**/*/').select { |fn| File.directory?(fn) }
    Intransition, Jul 4, 2009
    #4
  5. Mario Gr

    Mario Gr Guest

    Mario Gr, Jul 4, 2009
    #5
  6. Mario Gr

    Intransition Guest

    On Jul 3, 10:48=A0pm, Intransition <> wrote:
    > On Jul 3, 2:09=A0pm, Mario Gr <> wrote:
    >
    > > Hi,

    >
    > > I was wondering if there were a way to use Dir.glob to grab only
    > > directories. =A0After searching google for a bit I found ways to pull o=

    nly
    > > file listings, but couldn't find one for just directories.

    >
    > > Currently, I'm using FileTest.directory?(dir) before running any
    > > operations to test for a directory.

    >
    > Trailing '/':
    >
    > Dir.glob('*/').select { |fn| File.directory?(fn) }
    > Dir.glob('**/*/').select { |fn| File.directory?(fn) }


    Sorry, I meant:

    Dir.glob('*/')
    Dir.glob('**/*/')

    The trailing '/' makes the select unnecessary.
    Intransition, Jul 4, 2009
    #6
    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. Georgy Pruss
    Replies:
    15
    Views:
    723
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    356
    Duncan Booth
    Dec 1, 2003
  3. Matthew Denner
    Replies:
    1
    Views:
    178
  4. Une Bévue
    Replies:
    5
    Views:
    124
    7stud --
    Mar 6, 2008
  5. timr
    Replies:
    2
    Views:
    93
Loading...

Share This Page