Hello,
I have a perl script which deletes the folders by date but my goal is to check if a folder and file was created by capturing dates from folders and files and then execute the delete. If the folder and files do not exist then do nothing.
The below script only delete folders by date:
## Script to delete file older than * days
#!/usr/bin/perl
use POSIX qw(strftime);
use Win32;
use File::stat;
use Win32::OLE;
$Win32::OLE::Warn = 3;
$logdir="E:\\Scripts\\Logs";
$logfile=$logdir . "\\" ."clean.log";
$errfile=$logdir . "\\" ."clean.err";
close STDOUT;
open(STDOUT, '>>', $logfile);
close STDERR;
open(STDERR, '>>', $errfile);
print "\n\n................................\n";
my $now = localtime time;
print "$now\n";;
$Day = 10 ;
$hour = $Day * 24 ;
$Directory1 = "E:\\Scripts\\Backup\\database1" ;
printf ("Delete Folders under>> $Directory1 older than $hour hours\n\n");
opendir (DIR, "$Directory1/");
@DIR = readdir(DIR);
closedir (DIR);
chdir $Directory1;
## printf ("Directory1: $Directory1\n");
foreach $SubDir(@DIR) {
## printf ("SubDir: $SubDir\n");
if($SubDir =~ /.\b/)
{
$name1 = "$Directory1\\$SubDir";
## printf ("Name: $name1\n");
if (-M $name1 > $Day )
{
print "Deleting folder: $name1\n";
$objFSO = Win32::OLE->new('Scripting.FileSystemObject');
$objFSO->DeleteFolder($name1);
}
}
}
$Directory2 = "E:\\Scripts\\Backup\\database2" ;
printf ("\n\nDelete Folders under>> $Directory2 older than $hour hours\n\n");
opendir (DIR, "$Directory2/");
@DIR = readdir(DIR);
closedir (DIR);
chdir $Directory2;
## printf ("Directory2: $Directory2\n");
foreach $SubDir(@DIR) {
## printf ("SubDir: $SubDir\n");
if($SubDir =~ /.\b/)
{
$name1 = "$Directory2\\$SubDir";
## printf ("Name: $name1\n");
if (-M $name1 > $Day )
{
print "Deleting folder: $name1\n";
$objFSO = Win32::OLE->new('Scripting.FileSystemObject');
$objFSO->DeleteFolder($name1);
}
}
}
close STDOUT;
close STDERR;
I have a perl script which deletes the folders by date but my goal is to check if a folder and file was created by capturing dates from folders and files and then execute the delete. If the folder and files do not exist then do nothing.
The below script only delete folders by date:
## Script to delete file older than * days
#!/usr/bin/perl
use POSIX qw(strftime);
use Win32;
use File::stat;
use Win32::OLE;
$Win32::OLE::Warn = 3;
$logdir="E:\\Scripts\\Logs";
$logfile=$logdir . "\\" ."clean.log";
$errfile=$logdir . "\\" ."clean.err";
close STDOUT;
open(STDOUT, '>>', $logfile);
close STDERR;
open(STDERR, '>>', $errfile);
print "\n\n................................\n";
my $now = localtime time;
print "$now\n";;
$Day = 10 ;
$hour = $Day * 24 ;
$Directory1 = "E:\\Scripts\\Backup\\database1" ;
printf ("Delete Folders under>> $Directory1 older than $hour hours\n\n");
opendir (DIR, "$Directory1/");
@DIR = readdir(DIR);
closedir (DIR);
chdir $Directory1;
## printf ("Directory1: $Directory1\n");
foreach $SubDir(@DIR) {
## printf ("SubDir: $SubDir\n");
if($SubDir =~ /.\b/)
{
$name1 = "$Directory1\\$SubDir";
## printf ("Name: $name1\n");
if (-M $name1 > $Day )
{
print "Deleting folder: $name1\n";
$objFSO = Win32::OLE->new('Scripting.FileSystemObject');
$objFSO->DeleteFolder($name1);
}
}
}
$Directory2 = "E:\\Scripts\\Backup\\database2" ;
printf ("\n\nDelete Folders under>> $Directory2 older than $hour hours\n\n");
opendir (DIR, "$Directory2/");
@DIR = readdir(DIR);
closedir (DIR);
chdir $Directory2;
## printf ("Directory2: $Directory2\n");
foreach $SubDir(@DIR) {
## printf ("SubDir: $SubDir\n");
if($SubDir =~ /.\b/)
{
$name1 = "$Directory2\\$SubDir";
## printf ("Name: $name1\n");
if (-M $name1 > $Day )
{
print "Deleting folder: $name1\n";
$objFSO = Win32::OLE->new('Scripting.FileSystemObject');
$objFSO->DeleteFolder($name1);
}
}
}
close STDOUT;
close STDERR;