Built in Way to List Directories in a Directory in Ruby

Built in way to list directories in a directory in ruby

Starting from Chandra's answer, depending on whether you need or not the full path, you can use

Dir['app/*/']
# => ["app/controllers/", "app/helpers/", "app/metal/", "app/models/", "app/sweepers/", "app/views/"

Dir['app/*/'].map { |a| File.basename(a) }
# => ["controllers", "helpers", "metal", "models", "sweepers", "views"]

If you use Ruby >= 1.8.7, Chandra's answer can also be rewritten as

Pathname.glob('app/*/').map(&:basename)
# you can skip .to_s unless you don't need to work with strings
# remember you can always use a pathname as string for the most part of Ruby functions
# or interpolate the value

Getting a list of folders in a directory

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 == '..') }

Listing the files in a directory and all subdirectory

You might look at Dir.glob. You can pass it the **/* path which will give you everything in the folder and its subdirectories:

records = Dir.glob("path/to/your/root/directory/**/*")
# Will return everything - files and folders - from the root level of your root directory and all it's subfolders
# => ["file1.txt", "file2.txt", "dir1", "dir1/file1.txt", ...]

Since you probably want a list of files, excluding folders, you can use:

records = Dir.glob("path/to/your/root/directory/**/*").reject { |f| File.directory?(f) }

Get all file names in a directory with ruby

Dir.new('.').each {|file| puts file }

Note that this will include . and ..

One-liner to recursively list directories in Ruby?

Dir.glob("**/*/") # for directories
Dir.glob("**/*") # for all files

Instead of Dir.glob(foo) you can also write Dir[foo] (however Dir.glob can also take a block, in which case it will yield each path instead of creating an array).

Ruby Glob Docs

How to print list of directories and files and exclude names of directories?

Simply check if it's a file.

Dir.glob("**/*").each do |fname|
puts "<file href=\"#{fname}\" />" if File.file?(fname)
end

Get names of all files from a folder with Ruby

You also have the shortcut option of

Dir["/path/to/search/*"]

and if you want to find all Ruby files in any folder or sub-folder:

Dir["/path/to/search/**/*.rb"]

How to list directories and files within a RoR Ruby on Rails app a la Apache style?

Passenger can be configured so that it gets turned off in some folders:

<Location /shared>
PassengerEnabled off
Options +Indexes
</Location>

http://www.modrails.com/documentation/Users%20guide%20Apache.html#PassengerEnabled

Get list of subfolders from a given folder in JRuby

please check new stuff it producing expected result -

 records = Dir.glob('/E:/ISSUE_Folder/**/*.*')

records.each do |item|
puts File.dirname(item)
end

Sample Image

As you see its going to every folder and sub folder

Get immediate subdirectories in ruby

Dir.glob("**/") will return an array of all paths underneath the current directory. From there you can filter the list and copy a file with File.copy(from, to)



Related Topics



Leave a reply



Submit