C
Cosmic Cruizer
I'm looking for a peer review of the following code. I ran into quite a
few issues while developing it, so I think it best if I have additional
eyes looking at it.
There is a W2K3 drive that contains a folder that has multiple "job"
folders. Each job folder may contain additional folders, along with a
number of files. When the job folder's creation date exceeds a certain
number of days, the complete folder and all its contents need to be
deleted.
Here is my code. All feedback is welcomed.
#########################################################################
######
# File Name: directory_cleanup.pl
#
# WARNING: This script can be very unforgiving and damaging. Make sure
the
# the correct path is listed, otherwise extreme data loss my
occur!
#
# Assumes: Perl is installed on the Windows computer used to run this
script.
# (NOTE: This script was designed to run on a Windows
computer.) The
# user or account running this script has permissions to delete
all
# files and folders in the targeted path.
#
# This script removes folder structures that are past a listed number of
days.
# It will only analyze folders in the base directory and will ignore
files.
#
# $days is the number of days that are kept. All directories older than
$days
# will be deleted along with all of the contents of those directories.
#
# $base_dir is the base directory where the targeted directories are
checked
# for the date they were created and deleted if older than $days.
#
# A logfile is created on first use of this script and updated there
after
# each time this script runs. The current local time is added, then each
# directory that is deleted is recorded. If no directories are delete,
an
# entry noting "No directories deleted" is added.
#
# If a directory cannot be deleted, it will have two entries in the log:
# The first will say "Could not remove...", but then it will follow
that
# it was removed. It may be the case were most of the contents were
# removed, but not the final directory. In this case, remove the
directory
# manually. (You may need to unlock some items or they were in use.)
#
# NOTE: Once a directory and its files are deleted, they cannot be
undeleted!
#
# Created by: xxxxxxxxx
# Date: January 22, 2009
#
#########################################################################
######
use strict;
use File::Find;
my $base_dir='E:\Jobs'; # Add root name of folder to be
managed
my $days=31; # Change to number of days to keep
### Do not change anything below this line ###
my $output = 'delete_directories.txt';
my $deltime=time-$days*86400;
my $delete_flag = 0; # Flag to check for any deletions
my ($month, $day, $year) = (split / /, scalar localtime)[1,2,4];
open(OUT," >> $output") || die $!; # Open output file for logging
print OUT "$month $day, $year\n"; # Add current date to output file
opendir(DIR,$base_dir) || die $!; # Get list of directories
my @children = grep !/^\./, readdir (DIR);
closedir (DIR);
foreach my $child (@children) {
my $entry = $base_dir . '\\' . $child;
if (-d $entry){ # If the "entry" is a folder, process
it
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,
$ctime,$blksize,$blocks)=stat($entry);
my $ftime=$mtime;
if ($ftime<$deltime) { # If folder is older than $days,
remove it
# The next two lines finds the directory structure and does the
removal
finddepth (\&remove_dir, "$entry");
rmdir ( "$entry" ) or print OUT "\tCould not remove $entry\n";
$delete_flag = 1; # Set flag to record a directory was
deleted
print OUT "\t$entry\n"; # Write delete directory to log
file
}
}
}
print OUT "\tNo directories deleted\n", if($delete_flag eq 0);
print OUT "\n"; # Add a final line feed for readability
close(OUT);
exit;
### Subroutines ###
sub remove_dir { # Remove files and directories
# for a directory, this will be 0
if ( ! (stat("$File::Find::name"))[7] ) {
rmdir("$File::Find::name");
}
else {
unlink("$File::Find::name");
}
}
few issues while developing it, so I think it best if I have additional
eyes looking at it.
There is a W2K3 drive that contains a folder that has multiple "job"
folders. Each job folder may contain additional folders, along with a
number of files. When the job folder's creation date exceeds a certain
number of days, the complete folder and all its contents need to be
deleted.
Here is my code. All feedback is welcomed.
#########################################################################
######
# File Name: directory_cleanup.pl
#
# WARNING: This script can be very unforgiving and damaging. Make sure
the
# the correct path is listed, otherwise extreme data loss my
occur!
#
# Assumes: Perl is installed on the Windows computer used to run this
script.
# (NOTE: This script was designed to run on a Windows
computer.) The
# user or account running this script has permissions to delete
all
# files and folders in the targeted path.
#
# This script removes folder structures that are past a listed number of
days.
# It will only analyze folders in the base directory and will ignore
files.
#
# $days is the number of days that are kept. All directories older than
$days
# will be deleted along with all of the contents of those directories.
#
# $base_dir is the base directory where the targeted directories are
checked
# for the date they were created and deleted if older than $days.
#
# A logfile is created on first use of this script and updated there
after
# each time this script runs. The current local time is added, then each
# directory that is deleted is recorded. If no directories are delete,
an
# entry noting "No directories deleted" is added.
#
# If a directory cannot be deleted, it will have two entries in the log:
# The first will say "Could not remove...", but then it will follow
that
# it was removed. It may be the case were most of the contents were
# removed, but not the final directory. In this case, remove the
directory
# manually. (You may need to unlock some items or they were in use.)
#
# NOTE: Once a directory and its files are deleted, they cannot be
undeleted!
#
# Created by: xxxxxxxxx
# Date: January 22, 2009
#
#########################################################################
######
use strict;
use File::Find;
my $base_dir='E:\Jobs'; # Add root name of folder to be
managed
my $days=31; # Change to number of days to keep
### Do not change anything below this line ###
my $output = 'delete_directories.txt';
my $deltime=time-$days*86400;
my $delete_flag = 0; # Flag to check for any deletions
my ($month, $day, $year) = (split / /, scalar localtime)[1,2,4];
open(OUT," >> $output") || die $!; # Open output file for logging
print OUT "$month $day, $year\n"; # Add current date to output file
opendir(DIR,$base_dir) || die $!; # Get list of directories
my @children = grep !/^\./, readdir (DIR);
closedir (DIR);
foreach my $child (@children) {
my $entry = $base_dir . '\\' . $child;
if (-d $entry){ # If the "entry" is a folder, process
it
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,
$ctime,$blksize,$blocks)=stat($entry);
my $ftime=$mtime;
if ($ftime<$deltime) { # If folder is older than $days,
remove it
# The next two lines finds the directory structure and does the
removal
finddepth (\&remove_dir, "$entry");
rmdir ( "$entry" ) or print OUT "\tCould not remove $entry\n";
$delete_flag = 1; # Set flag to record a directory was
deleted
print OUT "\t$entry\n"; # Write delete directory to log
file
}
}
}
print OUT "\tNo directories deleted\n", if($delete_flag eq 0);
print OUT "\n"; # Add a final line feed for readability
close(OUT);
exit;
### Subroutines ###
sub remove_dir { # Remove files and directories
# for a directory, this will be 0
if ( ! (stat("$File::Find::name"))[7] ) {
rmdir("$File::Find::name");
}
else {
unlink("$File::Find::name");
}
}