Monks,

Wow, I haven't had to post a question in awhile. I guess I'm almost a half of monk : )

Anyways, I need some help with a Perl script I wrote for a fellow team member of mine. Here is a little background. They were basically going through a large file server (98GB) and manually finding files of certain extensions (.exe, .nsf, .mp3) and deleting them. They asked me to write a Perl script to do this automatically for them, which I was happy to do. They also asked if I could add a function to let them know how far along the script has gotten and to make sure it hasn't locked up(hence the print statements) while the script is running.

My question is, does the Locate_Files sub and Delete sub print out all of the matches it finds AND then go back through the filesystem and add the files to the array? Or does it print out the matches and then add the file to the array and move on to the next file? If not, what is the best way to do this? The script seemed to lock up the 1st time we ran it and I'm thinking this could have something to do with it, since it might be going through the server printing out all of the matches, then going back again and adding them to an array which seems like a stupid way of doing it.

You might also be wondering why I'm using File::Recurse. Well, I couldn't get File::Find to print out the absolute path of the file (ie. C:\data\test\ie.exe). Instead it would just print out the file name (ie.exe). I needed the whole path to be included in the email.

TIA for any help.
# use File::Recurse; use Mail::Sender; use strict; use warnings; my @files; my $addresses = 'jsmith@acme.com, jdoe@acme.com'; my $from_addr = 'perlscript@fileserver.acme.com'; # Search the diretory tree for the file types specified recurse(\&Locate_Files, "E:\\data"); # delete the files &delete(); # Send an email stating which files where deleted &send_mail(); ################# # Subroutines # ################# sub Locate_Files { if ( -f $_ ) { print "Found a match: $_\n" if ( $_ =~ /\.nsf$|\.exe$/i); push (@files,"$_") if ( $_ =~ /\.nsf$|\.exe$/i); } } #end Locate_Files sub sub delete { foreach (@files){ print "Deleting the following file: $_\n"; unlink || warn "Can not delete file $_: $!\n"; } } #end delete sub sub send_email { my $sender; ref ($sender = new Mail::Sender({from => "$from_addr", smtp => "apccorp"})) or die "$Mail::Sender::Error\n"; $sender->Open({to => "$addresses", subject => "Deleted Files Rep +ort"}); $sender->SendLine; $sender->Send(<<"END"); Team, Here is the list of files that where deleted the last time the Perl sc +ript was run on the data folder: @files END $sender->Close; } #end send_email sub

Thanks,
Dru
Another satisfied monk.

In reply to Best Way to Search and Delete files on a large Windows Filesystem by dru145

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.