# Formatting directory listings

**URL:** <https://www.ruby-forum.com/t/formatting-directory-listings/193075>\
**Category:** Ruby\
**Created:** [August 10, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/formatting-directory-listings/193075 "2010-08-10T20:26:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [August 10, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/formatting-directory-listings/193075/1 "2010-08-10T20:26:43Z")

</div>

Hi. Given an (imaginary) directory structure like so:

Movies/  
film1/film1.avi  
film2/film2.avi  
film3/film3.avi  
etc.

I’d like to get a directory listing like so:

# Movies

film1  
film2  
film3  
etc.

I don’t want anyone to actually do this for me, rather point me to a  
tutorial that deals with this sort of thing.

By the way, I know that:

films = Dir[’\*\*/\*.avi’]

will give me an array with the full pathnames. I’m after help with  
extracting the parent directory name.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [August 10, 2010, 8:59pm UTC](https://www.ruby-forum.com/t/formatting-directory-listings/193075/2 "2010-08-10T20:59:31Z")

</div>

On 10/08/10 20:25 , simonh wrote:

> Movies  
> By the way, I know that:
> 
> films = Dir[’\*\*/\*.avi’]
> 
> will give me an array with the full pathnames. I’m after help with  
> extracting the parent directory name.
> 
> Thanks for any help.  
> Checkout Rake::FileList and especially Rake::FileList#pathmap which will  
> go a long way towards doing what you want.  
> Cheers,  
> V.-

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [August 10, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/formatting-directory-listings/193075/3 "2010-08-10T22:08:02Z")

</div>

Thanks Vassilis. I’d prefer to not install anything to accomplish  
this. Also, I’d like to slightly to rephrase my aim:

1. Start off in ‘Movies’ directory.
2. Go into each subdir and get list of movies for that subdir. I’ve  
found how to just get the filename instead of full path: Dir[’\*\*/  
\*.avi’].collect { |vid| File.basename(vid) }
3. Store which movies are in each subdir.
4. Output the result.

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [August 10, 2010, 10:27pm UTC](https://www.ruby-forum.com/t/formatting-directory-listings/193075/4 "2010-08-10T22:27:57Z")

</div>

On Tue, Aug 10, 2010 at 4:05 PM, simonh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks Vassilis. I’d prefer to not install anything to accomplish  
> this. Also, I’d like to slightly to rephrase my aim:
> 
> 1. Start off in ‘Movies’ directory.
> 2. Go into each subdir and get list of movies for that subdir. I’ve  
> found how to just get the filename instead of full path: Dir[‘\*\*/  
> \*.avi’].collect { |vid| File.basename(vid) }

[http://ruby-doc.org/core/classes/File.html#M002542](http://ruby-doc.org/core/classes/File.html#M002542)

> 1. Store which movies are in each subdir.

movie\_store = {  
“directory1” =\> [“file1”, “file2”, “file3”],  
“directory2” =\> [“file4”, “file4”, “file6”]  
}

> 1. Output the result.

[…]

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [August 11, 2010, 12:52pm UTC](https://www.ruby-forum.com/t/formatting-directory-listings/193075/5 "2010-08-11T12:52:44Z")

</div>

simonh wrote:

> By the way, I know that:
> 
> films = Dir[’\*\*/\*.avi’]
> 
> will give me an array with the full pathnames. I’m after help with  
> extracting the parent directory name.

(1) You can strip it textually

path = “foo/bar/baz.avi”  
basename = path.sub(/^.\*//,’’)

(2) You can use the Pathname class

require ‘pathname’  
path = “foo/bar/baz.avi”  
basename = Pathname.new(path).basename.to\_s

(3) You can use Dir.foreach in a specific directory (but that won’t  
traverse arbitrary subdirectories like \*_/_ does)

Dir.foreach("/etc") { |b| puts b }

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [August 12, 2010, 12:12am UTC](https://www.ruby-forum.com/t/formatting-directory-listings/193075/6 "2010-08-12T00:12:47Z")

</div>

On Wed, 11 Aug 2010, simonh wrote:

> Thanks Vassilis. I’d prefer to not install anything to accomplish  
> this. Also, I’d like to slightly to rephrase my aim:
> 
> 1. Start off in ‘Movies’ directory.
> 2. Go into each subdir and get list of movies for that subdir. I’ve  
> found how to just get the filename instead of full path: Dir[’\*\*/  
> \*.avi’].collect { |vid| File.basename(vid) }
> 3. Store which movies are in each subdir.
> 4. Output the result.

Try this:

files = Hash.new { |hash, key| hash[key] = [] }

Dir[’\*\*/\*.avi’].each do |file|  
files[File.dirname(file)] \<\< File.basename(file)  
end

Not sure how you want the output formatted, but I believe that creates  
the data structure you’re looking for:

irb\> pp files  
{“movies/film1”=\>[“film1.avi”],  
“movies/film2”=\>[“film2.avi”],  
“movies/film3”=\>[“film3.avi”],  
“tv/mad\_men/season1”=\>[“episode3.avi”, “episode2.avi”,  
“episode1.avi”]}

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [August 12, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/formatting-directory-listings/193075/7 "2010-08-12T21:20:47Z")

</div>

Thanks very much mb… That’s just what I was after.
