in reply to 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?

Here is another attempt - This works ok using arrays, but it is not as clean as I would like. I derived this script from the Q&A section of this site with a few modifications.

This prints the output to the text file with comma delimiters. Is it possible to print to the text file using ROWS, so that each MP3 file listing is on a separate line on the output? Better readability. Importing to excel is an option, but I would like to view in the text file.....

use strict; use File::Find; open (MP3, ">c:/perltest/mp3test.txt"); 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 print join("\n", @foundfiles), "\n"; $, = ','; $/ = "\n"; print MP3 ("\n", @foundfiles), "\n"; close (MP3);
  • Comment on Re: Re: How do I write NT perl script to search mult. dirs and output to a file?
  • Download Code

Replies are listed 'Best First'.
(Ovid) Re(3): How do I write NT perl script to search mult. dirs and output to a file?
by Ovid (Cardinal) on Dec 26, 2001 at 22:50 UTC

    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.

      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...

      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

      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.