# Get folderlist in ruby

**URL:** <https://www.ruby-forum.com/t/get-folderlist-in-ruby/62094>\
**Category:** Ruby\
**Created:** [May 31, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/get-folderlist-in-ruby/62094 "2006-05-31T14:26:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@rlee](https://www.ruby-forum.com/u/rlee)\
**Post date:** [May 31, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/get-folderlist-in-ruby/62094/1 "2006-05-31T14:26:58Z")

</div>

Hi all,

i am quite new to ruby and I was wondering if there is a method in ruby  
to get a list of folders and subfolders.

e.g.: I have a ‘root’ directory and some sub directories and finaly some  
data files.

—root  
|—sub1  
| |----sub1A  
| | |----data.txt  
| |  
| |----sub1B  
| |----data.txt  
|  
|—sub2  
| |----sub2A  
| | |----data.txt  
. .  
. .  
. .  
and so on.

is there a way in ruby to get a list of all subdirectories, or maybe a  
string to each data.txt (this name doesnot change)

The problem is, that i don’t know how many ‘data.txt’ there are , and so  
i would need a method that searches in all subdirectories(starting at  
‘root’) for this file and returns an array of strings with the path to  
each file.

Is there maybe another way to do this?

Thanks a lot!

best regards,  
Roman

---

<div class="post-metadata">

**Author:** ![rlee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@rlee](https://www.ruby-forum.com/u/rlee)\
**Post date:** [May 31, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/get-folderlist-in-ruby/62094/2 "2006-05-31T14:34:34Z")

</div>

Roman L. wrote:

> Hi all,
> 
> i am quite new to ruby and I was wondering if there is a method in ruby  
> to get a list of folders and subfolders.

The standard Find module should let you do this.

require ‘find’

files = []  
Find.find( “root” ) do |path|  
files \<\< path if File.basename( path ) == “data.txt”  
end

---

<div class="post-metadata">

**Author:** ![rlee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@rlee](https://www.ruby-forum.com/u/rlee)\
**Post date:** [May 31, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/get-folderlist-in-ruby/62094/3 "2006-05-31T14:36:46Z")

</div>

2006/5/31, Roman L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> | |----sub1A
> . .
> 
> ```
> 
> and so on.
> 
> is there a way in ruby to get a list of all subdirectories, or maybe a  
> string to each data.txt (this name doesnot change)
> 
> The problem is, that i don’t know how many ‘data.txt’ there are , and so  
> i would need a method that searches in all subdirectories(starting at  
> ‘root’) for this file and returns an array of strings with the path to  
> each file.

files = Dir[‘root/\*\*/data.txt’]

> Is there maybe another way to do this?

Yes, you can also use Find:

require ‘find’  
Find.find( ‘root’ ) {|f| p f if File.basename(f) == “data.txt”}

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rlee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@rlee](https://www.ruby-forum.com/u/rlee)\
**Post date:** [May 31, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/get-folderlist-in-ruby/62094/4 "2006-05-31T15:31:34Z")

</div>

WOW!  
thanks for the very good answers!!!

I was searching in the file and DIR class to find something.  
well i am not yet familiar with ruby.  
but its amazing.

Thanks a lot to all of you!

Roman

---

<div class="post-metadata">

**Author:** ![rlee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@rlee](https://www.ruby-forum.com/u/rlee)\
**Post date:** [May 31, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/get-folderlist-in-ruby/62094/5 "2006-05-31T14:42:53Z")

</div>

On May 31, 2006, at 7:27 AM, Roman L. wrote:

> —root  
> . .  
> ‘root’) for this file and returns an array of strings with the path to  
> each file.

Neo:~/Desktop$ ruby search.rb  
root/sub1/sub1A/data.txt  
root/sub1/sub1B/data.txt  
root/sub2/sub2A/data.txt  
Neo:~/Desktop$ cat search.rb  
#!/usr/local/bin/ruby -w

def search( directory, pattern )  
result = Array.new  
Dir.glob("#{directory}/\*") do |file|  
next if file[0] == ?.  
if File.directory? file  
result.push(\*search(file, pattern))  
elsif file =~ pattern  
result \<\< file  
end  
end  
result  
end

puts search(“root”, /data.txt\Z/)

Hope that helps.

James Edward G. II
