in reply to Re: Re: How do I write NT perl script to search mult. dirs and output to a file?
in thread How do I write NT perl script to search mult. dirs and output to a file?

You're pretty close. The following should take care of this for you.

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.

  • Comment on (Ovid) Re(3): How do I write NT perl script to search mult. dirs and output to a file?
  • Download Code

Replies are listed 'Best First'.
Re: (Ovid) Re(3): How do I write NT perl script to search mult. dirs and output to a file?
by Rich36 (Chaplain) on Dec 26, 2001 at 23:22 UTC
    Should there be a > sign in both the open statement and the variable $report?
    Rich36
    There's more than one way to screw it up...

Re: (Ovid) Re(3): How do I write NT perl script to search mult. dirs and output to a file?
by rallen11 (Initiate) on Jan 02, 2002 at 20:40 UTC
    Thank you for assisting me in my journies! This script is working well for what I hoped it would do.

    However, it is a bit slow....but works. I would like to experiment using a hash, then reading the hash index for speed. Do you think that would speed it up a little?

    Also, if I were to put old mp3s in an old folder, could I perform an unlink on them? I'll give it the ole try...

    I'll keep you posted on my new and exciting journies.....

    Thanks again for all you've done to help get me "Started".

    Rob Allen II

Re: (Ovid) Re(3): How do I write NT perl script to search mult. dirs and output to a file?
by rallen11 (Initiate) on Jan 02, 2002 at 21:05 UTC
    Is there a way in Perl to "test" permissions or if the directory doesn't exist.

    Here are the errors that I keep getting when running on my network drives.

    Can't open k:/users/testuser: Bad file descriptor
    Can't open k:/users/testuser2: Bad file descriptor
    Can't open k:/users/testuser3/CHECK REQS: No such file or directory
    Can't open k:/users/testuser3/LABELS: No such file or directory
    Can't open k:/users/testuser3/MEMOS: No such file or directory
    Can't open k:/users/testuser4/BIO: No such file or directory

    The first error is bad file descriptor- Is that a permissions thing?

    The second error is no such file or directory- Is there a way to skip the directory if it doesn't exist? This slows down my script. Would creating a hash and reading the index alleviate this?

    Any input would be appreciated.