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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |