Forum: Ruby Using Dir.glob to grab only Directories

Posted by Mario Gr (mario517)
on 2009-07-03 20:09
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 by Andrea Fazzi (remogatto)
on 2009-07-03 20:43
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 by Robert Klemme (Guest)
on 2009-07-03 21:35
(Received via mailing list)
On 03.07.2009 20:43, Andrea Fazzi wrote:
> # 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
Posted by Thomas Sawyer (7rans)
on 2009-07-04 04:48
(Received via mailing list)
On Jul 3, 2:09 pm, Mario Gr <mflor...@gmail.com> 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.

Trailing '/':

Dir.glob('*/').select { |fn| File.directory?(fn) }
Dir.glob('**/*/').select { |fn| File.directory?(fn) }
Posted by Mario Gr (mario517)
on 2009-07-04 07:58
Excellent.  Thanks for the help.
Posted by Thomas Sawyer (7rans)
on 2009-07-04 22:09
(Received via mailing list)
On Jul 3, 10:48 pm, Intransition <transf...@gmail.com> wrote:
>
> 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.
Posted by Flower Born (flowerborn)
on 2009-07-05 05:47
(Received via mailing list)
This is cool - is there a way to grab only non-directories (or regular
files) ?

Thanks,
Jan

* Intransition <transfire@gmail.com> [2009-07-04 11:48:16 +0900]:
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.