Formatting directory listings

S

simonh

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.
 
V

Vassilis Rizopoulos

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.
Checkout Rake::FileList and especially Rake::FileList#pathmap which will
go a long way towards doing what you want.
Cheers,
V.-
 
S

simonh

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.
 
B

brabuhr

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

3. Store which movies are in each subdir.

movie_store = {
"directory1" => ["file1", "file2", "file3"],
"directory2" => ["file4", "file4", "file6"]
}
4. Output the result.

[...]
 
B

Brian Candler

simonh said:
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 }
 
M

mberg

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"]}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top