use strict;
use File::Find;
my $report = "c:/perltest/mp3test.txt";
open MP3, "> $report" or die "Cannot open $report for writing: $!";
my @directories = (".", "c:/");
my @foundfiles;
# Here, we collect all .mp3 files below each directory in @directories
# and put them into @foundfiles
find( sub { push @foundfiles, $File::Find::name if /\.mp3$/ }, @direct
+ories );
# and output them all
my $mp3s = join("\n", @foundfiles), "\n";
print $mp3s;
print MP3 $mp3s;
close MP3;
Note that I took out hte assignment to $/. That's the input field separator and is used when reading a file. I also took out the assignment to $, as we don't need it with this. I joined the files you found with a newline, assigned that to the variable $mp3s and both print that to the screen and to the file. No duplicate code that way. I also added an "or die" statement to you open. That's a good habit to get into in the future.
Good luck with your Perl journies!
Cheers,
Ovid
Update: Fixed the typo that Rich36 caught. Good job!
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats. |