Scheduled deletion of old files

December 12th, 2005

I use Juice to automatically download podcast files (MP3s) into C:\Documents and Settings\ac\My Documents\XDA6 My Documents\. ActiveSync then automatically uploads these files onto my PDA when I connect it to the PC. The result is that I always have lots of new podcasts to listen to on my PDA.

All well and good but I have limited space on my storage card, so I need to keep deleting the files from my PC, or they get loaded onto my PDA - just deleting them from the PDA isn't enough because they just reappear next time the PDA syncs with the PC.

What I need is for the old files to be deleted automatically, so that they don't accumulate.

To do this I created (or more correctly, modified) a script that deletes files based on their modification date. I then created a Windows schedule to run this script every day.

Here are the details.

[NOTE: See also the new, improved version: Tuesday, 20 December.]


There are 3 elements to this:
1) A Windows batch file
2) The script - a small Perl script.
3) A Windows "Scheduled Tasks" entry.

The batch file (run-deleter-script.bat) is simply used to run the Perl script.

The Perl script (deleter.pl) deletes old files in a specified directory. The age of the files to be deleted and the directory in and under which all files are deleted is specified in the batch file.

The reason I use a batch file, rather than just running the Perl script is that this way the command console window stays open, telling me what files have been deleted.

There's another version of the Perl script (deleter-nonrecursive.pl) that only deletes files in the specified directory, and doesn't recurse down into the child directories looking for & deleting old files.

The original source for the Perl scripts is given in my version of the scripts - see below.

To set up the Windows scheduled task go to:
Start > Programs > Accessories > System Tools > Scheduled Tasks
In the wizard choose the batch file and set up how often you want it to run. Unfortunately you're limited to a maximum frequency of once a day.

NOTES:
1) If you don't already have Perl on your PC, download the free Standard Distribution from www.activestate.com.
2) Before you run these, I'd recommend setting the script to print out the list of files only, and not delete anything until you're sure it's working properly. To test the script and batch file, don't use the scheduler, just double-click the batch file in Windows Explorer.

The scripts

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

# To simply print the names of files,
# instead of actually deleting them (i.e. for testing),
# comment out the line containing: 'unlink $_'

# To delete the files without printing a list of matched files,
# comment out the line containing: 'print "$_\n"'

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

my $age = $ARGV[0];
my $dir = $ARGV[1] || '.';

print "\nThe following files have been deleted:\n";
find(sub { print "$_\n" if -M $_ > $age; }, $dir);
find(sub { unlink $_ if -M $_ > $age; }, $dir);

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 from the
  c:\temp directory that are older than 120 days.
EOF
exit;
}

# Credit:
# This script was based heavily on a script posted by "FishMonger"
# on the Computing.net forum:
# http://computing.net/programming/wwwboard/forum/13717.html
# on December 06, 2005 at 16:22:31 Pacific

# Modified 12 December 2005 by Alistair Christie (www.itauthor.com/notes).

deleter-nonrecursive.pl


#!perl -w

use strict;

# This script deletes all files older than a specified age
# in a specified directory (or the current directory if none specified).

# To simply print the names of files,
# instead of actually deleting them (i.e. for testing),
# comment out the line containing: 'unlink $_'

# To delete the files without printing a list of matched files,
# comment out the line containing: 'print $_'

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

my $age = $ARGV[0];
my $dir = $ARGV[1] || '.';

print "\nThe following files have been deleted:\n";
while (<$dir/*>) { print "$_\n" if -M $_ > $age; }
while (<$dir/*>) { unlink $_ if -M $_ > $age; }

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 from the
  c:\temp directory that are older than 120 days.
EOF
exit;
}

# Credit:
# This script was based heavily on a script posted by "FishMonger"
# on the Computing.net forum:
# http://computing.net/programming/wwwboard/forum/13717.html
# on December 06, 2005 at 16:22:31 Pacific

# Modified 12 December 2005 by Alistair Christie (www.itauthor.com/notes).

run-deleter-script.bat


@echo off
REM Open a console window (using /k flag so that it gets left open)

@echo on
cmd /k perl deleter.pl 1 "C:\Documents and Settings\ac\My Documents\XDA6
My Documents\podcasts"

Comments are closed.