in reply to Re: how to get the parent directory name?
in thread how to get the parent directory name?

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.

Replies are listed 'Best First'.
Re^3: how to get the parent directory name?
by Anonymous Monk on Dec 08, 2006 at 14:45 UTC
    Thanks all of you for the resposes!

    I used this code ast a starting point, and I'm almost there, thanks!

    I had to change
    next if (-D
    into
    next if (!-d
    I guess that's just my version of perl?