Use Recursion. I built the following script to recurse through my mp3 directories to build a playlist. I use this on Win32 primarily, but it works on linux/unix too. Just specify the separator on the command line when you run it.
#!/usr/bin/perl
use MP3::Info;
$dHandle = 'dh00';
my $startDir = shift || '.';
my $separator = shift || '\\';
parseDir($startDir);
sub parseDir
{
my $currentDir = shift || return;
my $dirHandle = $dHandle++;
opendir($dirHandle, $currentDir) or die "Coudln't open directory $
+startDir.\n";
while (my $filename = readdir ($dirHandle))
{
next if ($filename =~ /^\./); # go to next filename if we have
+ a special file (i.e. . or ..)
# if we find a directory, parse it by calling the pars
+eDir again ( Recursion ).
if( -d "$currentDir$separator$filename" )
{
parseDir("$currentDir$separator$filename");
}
elsif($filename =~ /mp3/i)
{
my $mp3 = new MP3::Info "$currentDir$separator$filename";
unless ($mp3 == undef)
{
print "$currentDir$separator$filename\n";
}
}
}
closedir($dirHandle);
}
exit(0);
Notice the line: $dHandle = 'dh00';
This variable gets incremented and the subsequent value value gets used as the directory handle in the current iteration.
Let me know if you have more questions about this.
-Matt
Edit by tye
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.