For cases like this, I work from the top, and just keep track of where I've been:

my $basedir = '/top/of/the/tree/'; opendir( ARTISTS, $basedir ) or die "Can't read from $basedir : $!"; foreach my $artist ( readdir(ARTISTS) ) { next if (-D "$basedir/$artist" or $artist =~ m/^[.]*$/); opendir( ALBUMS, "$basedir/$artist" ) or warn "Can't read from $basedir/$artist : $!" and next; foreach my $album ( readdir(ALBUMS) ) { next if (-D "$basedir/$artist/$album" or $album =~ m/^[.]*$/); opendir ( TRACKS, "$basedir/$artist/$album" ) or warn "Can't read from $basedir/$artist/album : $!" and next; foreach my $track ( readdir(TRACKS) ) { next if ($track =~ m/^[.]*$/); ... } } }

Yes, it seems like it's more work, but it allows you do special handling as you enter/leave each directory -- maybe you want to create playlists for each artists, etc. And I'm actually dealing with millions of files (scientific data, not music albums)

okay ... I don't actually use code like that -- it's too repetitive ... I use recursive functions (as it's much more than 3 levels deep) and a dispatch table for the directories that need to be handled in a special manner ... but the above at least explains the basic methodology and should be easier to follow.


In reply to Re^2: how to get the parent directory name? by jhourcle
in thread how to get the parent directory name? by juro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.