Scheduled deletion of old files – improved

December 20th, 2005

I've tweaked the script in my previous posting. The method described previously left a command console open displaying the files that had been deleted. This worked OK, but wasn't terribly smart because I'm not in the office every day, and the program didn't exit until I'd closed the console window.

A better way is to get the script to email the list of which files had been deleted. This is what the following script does. It also dispenses with the need for a batch file. The Windows scheduled task can then call the script directly. In my case, the scheduled task executes the command:

C:\Perl\bin\perl.exe D:\MyStuff\PerlScripts\deleter-scripts\deleter.pl 1 "C:\Documents and Settings\ac\My Documents\XDA6 My Documents\podcasts"

The script relies on you being on a network on which a mail server is available. That way sendmail is able to work its magic and deliver the email for you. All you need to do is change the value of $mailto to the email address you want the message to be sent to.

The revised script:

deleter.pl


#!perl -w

use strict;
use File::Find;

# This script deletes all files older than a specified age
# in, ***or below***, a specified directory
# (or the current directory if none specified)
# and emails the results to the address given in the script. 

# To test the script (i.e. list files that would have been
# deleted, without actually deleting them),
# comment out the line containing "unlink $_".

usage() if (! @ARGV || $ARGV[0] =~ /\D/);

my $age = $ARGV[0];
my $dir = $ARGV[1] || '.';
my $message  = "\nThe following files have been deleted:\n";
my $files    = "";
### If you're running it from SFU use this path:
### my $sendmail     = "/usr/sbin/sendmail";
### If you're running it from a Windows shell use this path:
my $sendmail     = "C:\\SFU\\usr\\sbin\\sendmail";
my $mailsubject  = "Old podcast files deleted";
my $mailto       = "you\@your.com";

find(sub { $files .= "$_\n" if (-M $_ > $age && $_ ne "."); }, $dir);
find(sub { unlink $_ if (-M $_ > $age && $_ ne "."); }, $dir);

$message="\nNo files were deleted.\n\n" unless $files;
print $message.$files;

open (MAIL, "|$sendmail -t ") or die "Cannot open $sendmail: $!";
        print MAIL "From: \"deleter.pl script\" <$mailto>\n";
        print MAIL "To: $mailto\n";
        print MAIL "Content-Type: text/plain\n";
        print MAIL "Subject: $mailsubject\n\n";
        print MAIL $message.$files;
close (MAIL);

sub usage {
print <<'EOF';

USAGE:
========
<scriptname> n [directory]

Where:
- scriptname is the name of this Perl script.
- n is the number of days previous to today.
- directory is an optional parameter that specifies
  the directory where files exist.
  Note: the default is the current directory.

For example:
- "perl deleter.pl 5" - deletes files older than 5 days.
- "perl deleter.pl 120 c:\temp" - deletes files in or below the
  c:\temp directory that are older than 120 days.
EOF
exit;
}

I have this script scheduled to be run once a day at 5.00 am. Here's an example of the email that's waiting for me in my inbox every morning:

deleter.pl-email.png

Potentially similar posts

Comments are closed.