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

Learn to use File::Find to search for the files and open a file in write mode and print to the filehandle any results that you have found.

I have, on more than one occassion, rewritten a fellow Monks code from the ground up -- provided they show that they have put some effort into it. I don't write code for people who don't post code. If you have any sample code to post, do so and we'll take a look at it and see what we can do to help.

  • Comment on Re: How do I write NT perl script to search mult. dirs and output to a file?

Replies are listed 'Best First'.
Re: Re: How do I write NT perl script to search mult. dirs and output to a file?
by Anonymous Monk on Dec 26, 2001 at 21:14 UTC
    Thank you for responding. I'm continuing my learning by reading PERL books and sites like this one. Here is my attempt at accomplishing my mult-directory search of my C: drive for MP3 files. I first tried to do a simple array, which is commented out at the bottom of the script, but found that I couldn't get into sub-directories. I would like to output my search into the text file for review, then if I decide to go ahead and delete, I can un-comment my UNLINK statement. Any help would be appreciated....

    I keep getting the following SYNTAX error: Can't locate File/Recurse.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/li b .) at C:\Rob\JHMCIS Scripts\MP3_Deletion.pl line 1. BEGIN failed--compilation aborted at C:\Rob\JHMCIS Scripts\MP3_Deletion.pl line 1.

    *************************************
    Here's my attempt:

    open (MP3, ">c:\\perltest\\mp3log.txt"); recurse(\&Locate_MP3, "c:\\ "); close (MP3); sub Locate_MP3 # Recurses directory structure, #locates matching files, and deletes them(print to log #first, unlink +later after review). { if ( -f $_ ) { if ( $_ =~ /\.mp3$/i ) { # unlink ("$_") } } } print MP3 “$_ matches deletion criteria.\n”; # This was my first attempt at an array to do the same # thing. Only problem....only works on current directory. #open (MP3, ">c:\\perltest\\mp3test.txt"); #$timenow = time; #@mp3list = <c:\\perltest\\*.mp3>; #print MP3 "@mp3list\n"; #close (MP3);

    Thanks,
    Seeker of Perl Wisdom

Re: Re: How do I write NT perl script to search mult. dirs and output to a file?
by rallen11 (Initiate) on Dec 26, 2001 at 22:28 UTC
    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);

      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.

        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.

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