# Getting all directories in a specified directory

**URL:** <https://www.ruby-forum.com/t/getting-all-directories-in-a-specified-directory/76672>\
**Category:** Rails\
**Created:** [November 20, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/getting-all-directories-in-a-specified-directory/76672 "2006-11-20T14:47:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![al\_schmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@al\_schmid](https://www.ruby-forum.com/u/al_schmid)\
**Post date:** [November 20, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/getting-all-directories-in-a-specified-directory/76672/1 "2006-11-20T14:47:02Z")

</div>

Hello everyone

I’m trying to make a “directory chooser” for a form, so that the user  
can choose a subdirectory (via dropdown) of a specified root directory  
(under public/images) on the server. Sooo… how do I get an array or a  
hash of all the subdirectories in a specified directories?  
And: how can I then get an array or a hash of all the files that are in  
that subdir?

Thanks a lot!  
al

---

<div class="post-metadata">

**Author:** ![al\_schmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@al\_schmid](https://www.ruby-forum.com/u/al_schmid)\
**Post date:** [November 20, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/getting-all-directories-in-a-specified-directory/76672/2 "2006-11-20T14:47:37Z")

</div>

On Nov 15, 2006, at 8:28 AM, al schmid wrote:

> that subdir?  
> Try this:

def parse\_dir(path)  
dirs, files = [], []  
Dir.entries(path).each do |p|  
dirs \<\< p if File.directory?§  
files \<\< p if File.file?§  
end  
dirs[0…2] = [] # removes ‘.’ and ‘…’ directories (if on UNIX)  
[dirs,files]  
end

directories, files = parse\_dir(some\_path)

Call additional times with a new path to get subdirectory information.

cr

---

<div class="post-metadata">

**Author:** ![al\_schmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@al\_schmid](https://www.ruby-forum.com/u/al_schmid)\
**Post date:** [December 15, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/getting-all-directories-in-a-specified-directory/76672/3 "2006-12-15T22:42:25Z")

</div>

Hi

Sorry if this is coming to the list a second time…

I’m new to Ruby on Rails and also new here, so apologies if this is  
something obvious I’m missing.

I’m also trying to list the contents of a directory and separate them  
into files and dirs. The ruby script I’ve written, as well as the one  
here, both work fine when run from the terminal. However, when I try  
to use either in Rails, the app only recognizes … and . as directories  
and .DS\_Store as a file, even though it can list the complete contents  
of the directory.

Any thoughts? Thanks for any help.

cheers  
dafydd

---

<div class="post-metadata">

**Author:** ![al\_schmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@al\_schmid](https://www.ruby-forum.com/u/al_schmid)\
**Post date:** [December 15, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/getting-all-directories-in-a-specified-directory/76672/4 "2006-12-15T23:00:29Z")

</div>

dafydd wrote:

> I’m also trying to list the contents of a directory and separate them  
> into files and dirs. The ruby script I’ve written, as well as the one  
> here, both work fine when run from the terminal. However, when I try  
> to use either in Rails, the app only recognizes … and . as directories  
> and .DS\_Store as a file, even though it can list the complete contents  
> of the directory.

Perhaps this code snippet will help. Also try logging/outputting the  
result of Dir.pwd, i.e. where is your current working directory under  
each circumstance?

class Dir  
def self.crawl( path, max\_depth=nil, include\_directories=false,  
depth=0, &block )  
return if max\_depth && depth \> max\_depth  
begin  
if File.directory?( path )  
yield( path, depth ) if include\_directories  
files = Dir.entries( path ).select{ |f| f[0,1]!=’.’ }  
unless files.empty?  
files.collect!{ |file\_path|  
Dir.crawl( path+’/’+file\_path, max\_depth,  
include\_directories, depth+1, &block )  
}.flatten!  
end  
return files  
else  
yield( path, depth )  
end  
rescue SystemCallError =\> the\_error  
warn “ERROR: #{the\_error}”  
end  
end

end

---

<div class="post-metadata">

**Author:** ![al\_schmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@al\_schmid](https://www.ruby-forum.com/u/al_schmid)\
**Post date:** [December 16, 2006, 12:44am UTC](https://www.ruby-forum.com/t/getting-all-directories-in-a-specified-directory/76672/5 "2006-12-16T00:44:48Z")

</div>

Hey thanks!

I have to admit the code snippet is a bit beyond my grasp at the  
moment, and I’m gonna have to spend some time figuring it out, but in  
the meantime I followed your current directory tip and taht fixed the  
trouble. As far as I can figure, I needed to actually bbe working in  
the directory - it wasn’t enough to just provide the pathname.

Thanks again.

cheers  
dafydd

On 12/15/06, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> result of Dir.pwd, i.e. where is your current working directory under  
> unless files.empty?  
> warn “ERROR: #{the\_error}”  
> end  
> end
> 
> end
> 
> >

–  
www.sideshowmedia.ca  
skype: chickeninthegrass
