Peer Review for Folder Delete Script



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");
}
}

.



Relevant Pages

  • Re: User account and Personal folder mismatch script
    ... I believe you want a script that can be run by an administrator that will ... The script above only documents if a home folder has been assigned. ... > End Sub ...
    (microsoft.public.scripting.vbscript)
  • Input on CleanUp script
    ... Since I'm going to be using this script in a production environment, ... If the script is used with a backup folder ... var strTargetFolder, strBackupFolder, args0, args1, args2, noArgs; ...
    (microsoft.public.scripting.wsh)
  • Input on CleanUp script
    ... Since I'm going to be using this script in a production environment, ... If the script is used with a backup folder ... var strTargetFolder, strBackupFolder, args0, args1, args2, noArgs; ...
    (microsoft.public.scripting.jscript)
  • Re: Disappearing Links
    ... I believe someone has a script for rebuilding links but I'm unable to find it at the moment. ... It could be adapted to trawl through the whole mail folder tree - if you really need that, ... -- to any contacts in the address book that match the sender of the messages ... set theSelection to selection ...
    (microsoft.public.mac.office.entourage)
  • Re: OT:macros or software?
    ... when a .PDF file is stored there, ... words and depending on weather those words are found in that document, move it to a predetermined folder. ... Easy enough if it were a text document, or even Word, as you could write a DOS script, or a VBS script if you had to load Word to read Word documents, although writing such a script can be harder to a beginner than some might think. ... OK the goal is to put the numerous files scattered everywhere on the computer at work into easy to use folders for my staff to access simply. ...
    (uk.comp.homebuilt)

Loading