Retrieving a list of folders in a directory

How to get a list of folders that exist in a specific directory with ruby?

Dir.entries() looks close, but I don't know how to limit only folders.

+81
ruby directory
Dec 14 '09 at 5:26
source share
13 answers

Jordan is close, but Dir.entries does not return the full path that File.directory? expects File.directory? . Try the following:

  Dir.entries('/your_dir').select {|entry| File.directory? File.join('/your_dir',entry) and !(entry =='.' || entry == '..') } 
+64
Dec 14 '09 at 6:02
source share

I found this more useful and easy to use:

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

it gets all the folders in the current directory except . and ..

To browse folders simply use ** instead of * .

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

 Dir.chdir('/destination directory') do Dir.glob('*').select { |f| File.directory? f } end 
+98
May 30 '11 at 20:20
source share

In my opinion, Pathname much better for file names than simple strings.

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

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

If you want to have lines

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

If directory_name was absolute, these lines are also absolute.

+44
Dec 14 '09 at 9:27
source share

Recursively find all folders in a specific directory:

 Dir.glob 'certain_directory/**/*/' 

Non-recursive version:

 Dir.glob 'certain_directory/*/' 

Note: Dir.[] Works like Dir.glob .

+20
Mar 18 '13 at 9:09
source share

Can you use File.directory? from the FileTest module to find out if the file is a directory. Combining this with Dir.entries makes a nice (ish) -liner:

 directory = 'some_dir' Dir.entries(directory).select { |file| File.directory? File.join(directory, file} 

Edit: Updated by ScottD correction.

+4
Dec 14 '09 at 5:34
source share
 directory = 'Folder' puts Dir.entries(directory).select { |file| File.directory? File.join(directory, file)} 
+4
Dec 14 '09 at 9:05
source share
 Dir.glob('/your_dir').reject {|e| !File.directory?(e)} 
+1
Aug 26 '13 at 18:16
source share
 $dir_target = "/Users/david/Movies/Camtasia 2/AzureMobileServices.cmproj/media" Dir.glob("#{$dir_target}/**/*").each do |f| if File.directory?(f) puts "#{f}\n" end end 
+1
May 15 '14 at 10:00
source share

For a general solution, you probably want to use

 Dir.glob(File.expand_path(path)) 

This will work with paths like ~/*/ (all folders in your home directory).

+1
Aug 25 '17 at 15:12
source share

Only folders ("." AND ".." are excluded):

Dir.glob(File.join(path, "*", File::SEPARATOR))

Folders and files:

Dir.glob(File.join(path, "*"))

+1
Jan 26 '18 at 11:17
source share

With this, you can recursively get an array of the full path to your directories, subdirectories, subdirectories. I used this code to load these files into the config/application file.

 Dir.glob("path/to/your/dir/**/*").select { |entry| File.directory? entry } 

In addition, we do not need to deal with boring . and .. more. The accepted answer was to deal with them.

+1
Jun 06 '19 at 1:37
source share

I think you can test each file to see if it is a directory with FileTest.directory? (file_name) FileTest.directory? (file_name) . See the documentation for FileTest for more information.

0
Dec 14 '09 at 5:32
source share

We can combine Borh 's answer with johannes's answer to get a pretty elegant solution for getting the directory names in a folder.

 # user globbing to get a list of directories for a path base_dir_path = '' directory_paths = Dir.glob(File.join(base_dir_path, '*', '')) # or recursive version: directory_paths = Dir.glob(File.join(base_dir_path, '**', '*', '')) # cast to Pathname directories = directory_paths.collect {|path| Pathname.new(path) } # return the basename of the directories directory_names = directories.collect {|dir| dir.basename.to_s } 
0
Dec 07 '17 at 3:26
source share



All Articles