Formatting directory listings

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.

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

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.

On Tue, Aug 10, 2010 at 4:05 PM, simonh [email protected]
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

  1. Store which movies are in each subdir.

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

  1. Output the result.

[…]

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 }

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs