From: mberg on


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


From: simonh on
Thanks very much mb... That's just what I was after.