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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With