Formatting directory listings

Discussion in 'Ruby' started by simonh, Aug 10, 2010.

  1. simonh

    simonh Guest

    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.
    simonh, Aug 10, 2010
    #1
    1. Advertising

  2. On 10/08/10 20:25 , simonh wrote:
    > 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.-

    --
    http://www.ampelofilosofies.gr
    Vassilis Rizopoulos, Aug 10, 2010
    #2
    1. Advertising

  3. simonh

    simonh Guest

    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.
    simonh, Aug 10, 2010
    #3
  4. simonh

    Guest

    On Tue, Aug 10, 2010 at 4:05 PM, 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) }


    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.


    [...]
    , Aug 10, 2010
    #4
  5. 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 }
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Aug 11, 2010
    #5
  6. simonh

    Guest

    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"]}
    , Aug 11, 2010
    #6
  7. simonh

    simonh Guest

    Thanks very much mb... That's just what I was after.
    simonh, Aug 12, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brent
    Replies:
    1
    Views:
    437
    Craig Deelsnyder
    Jun 10, 2005
  2. Blogger Team

    Free Listings in Blog directory

    Blogger Team, Nov 27, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    356
    Blogger Team
    Nov 27, 2005
  3. Chris McMahon
    Replies:
    11
    Views:
    703
    Chris McMahon
    Jun 27, 2005
  4. Dan King

    Dynamic Directory Listings

    Dan King, Aug 2, 2004, in forum: ASP General
    Replies:
    2
    Views:
    119
    Dan King
    Aug 2, 2004
  5. Carolyn Speakman

    Getting directory listings from a client machine

    Carolyn Speakman, Aug 20, 2004, in forum: ASP General
    Replies:
    1
    Views:
    255
    Jeff Cochran
    Aug 20, 2004
Loading...

Share This Page