Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Using Dir.glob to grab only Directories

Reply
Thread Tools

Using Dir.glob to grab only Directories

 
 
Mario Gr
Guest
Posts: n/a
 
      07-03-2009
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/.

 
Reply With Quote
 
 
 
 
Andrea Fazzi
Guest
Posts: n/a
 
      07-03-2009
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/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      07-03-2009
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/
 
Reply With Quote
 
Intransition
Guest
Posts: n/a
 
      07-04-2009


On Jul 3, 2:09=A0pm, Mario Gr <(E-Mail Removed)> 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) }


 
Reply With Quote
 
Mario Gr
Guest
Posts: n/a
 
      07-04-2009
Excellent. Thanks for the help.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Intransition
Guest
Posts: n/a
 
      07-04-2009


On Jul 3, 10:48=A0pm, Intransition <(E-Mail Removed)> wrote:
> On Jul 3, 2:09=A0pm, Mario Gr <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
virtual directories/physical directories in IIS Paul F ASP .Net 4 06-25-2008 04:04 PM
Multiple bin-directories with virtual directories? =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?= ASP .Net 0 11-09-2004 05:49 PM
Getting all directories/files from current directory and using -d flag for the directories Adam Petrie Perl Misc 8 10-11-2004 01:28 PM
How to map Project directories to Production sub-directories Joel Finkel ASP .Net 0 09-12-2003 06:47 PM
Using virtual directories for common directories (scripts, images, styles, etc.) Jeffry van de Vuurst ASP .Net 2 07-30-2003 07:00 PM



Advertisments