Monday, December 19, 2005

Inconvenient Temp Files

Recently I had a temp directory that shut down a server by filling up the C drive. The culprit was a nightly job that unzipped temp files into the temp directory (Go Figure ;-)). The solution was simple. Create a Perl job to kill those files nightly before that other job runs. The code follows:

# This program get's rid of all the files matching regex $regex (ckz_* in our case)
# temp files for user ARGV[0]

use File::Path;
my $user = $ARGV[0];
my $regex = $ARGV[1];
my $temp_dir = "C:\\Documents and Settings\\$user\\Local Settings\\Temp";
if (-d $temp_dir) {

      opendir(DE,$temp_dir);
my @all_files = readdir(DE);
foreach $file (@all_files) {
# Make sure file is not a directory
$entry = "$temp_dir\\$file";
if (-d $entry) {
if ($entry =~m/$regex/i) {
print "DELETING ENTRY: $entry\n";
rmtree($entry);
}
}
}
closedir(DE);
}

else {

print "Directory $temp_dir Does Not Exist!";
}

This code removes any temp files matching $regex in the user $user temp files. These two variables are passed in on the command line ($ARGV[0],$ARGV[1]).

No comments: