Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of folders in a directory

How do I get a list of the folders that exist in a certain directory with ruby?

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

like image 278
0xC0DEFACE Avatar asked Dec 14 '09 05:12

0xC0DEFACE


People also ask

How do I get a list of folders in a folder?

You can use the DIR command by itself (just type “dir” at the Command Prompt) to list the files and folders in the current directory.


1 Answers

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 
like image 56
Emiliano Poggi Avatar answered Sep 30 '22 01:09

Emiliano Poggi