From: simonh on
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.


From: Vassilis Rizopoulos on
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


From: simonh on
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.

From: brabuhr on
On Tue, Aug 10, 2010 at 4:05 PM, simonh <simonharrison.uk(a)gmail.com> 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.

[...]

From: Brian Candler on
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/.