rallen11 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How do I write NT perl script to search mult. dirs and output to a file?

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

    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.

      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

      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.

Re: How do I write NT perl script to search mult. dirs and output to a file?
by rbc (Curate) on Dec 20, 2001 at 05:49 UTC
    If your a person that likes Unix utils
    like find, vi, sed, grep but your boss
    requires you to use a NT box you could
    look into Cygwin ...

    http://www.cygwin.com/

    ... it works pretty well.
    You could then do cools things
    like ...

    bash$ find . -name "*.pl" > output

    --
    Its like a dog that can sing and dance.
    It's remarkable because it can do it.
    Not that it can do it well.