Getting a list of folders in a directory

I've found this more useful and easy to use:

Dir.chdir('/destination_directory')
Dir.glob('*').select {|f| File.directory? f}

it gets all folders in the current directory, excluded . and ...

To recurse folders simply use ** in place of *.

The Dir.glob line can also be passed to Dir.chdir as a block:

Dir.chdir('/destination directory') do
  Dir.glob('*').select { |f| File.directory? f }
end

In my opinion Pathname is much better suited for filenames than plain strings.

require "pathname"
Pathname.new(directory_name).children.select { |c| c.directory? }

This gives you an array of all directories in that directory as Pathname objects.

If you want to have strings

Pathname.new(directory_name).children.select { |c| c.directory? }.collect { |p| p.to_s }

If directory_name was absolute, these strings are absolute too.


Jordan is close, but Dir.entries doesn't return the full path that File.directory? expects. Try this:

 Dir.entries('/your_dir').select {|entry| File.directory? File.join('/your_dir',entry) and !(entry =='.' || entry == '..') }