Sunday, February 6, 2011

PERL : Check For new files

pico checkfornewfiles.pl
#!/usr/bin/perl -w
use strict;
AscertainStatus();


sub AscertainStatus
{
my $DIR = "test2";
opendir (HAN1, "$DIR") || die "Problems: $!";
my @array1 = readdir(HAN1);
if("$#array1" > 1)#we know we have a new files. by default folders '.' and '..' exists.
{
for(my $i = 0 ; $i < 2 ; $i ++)
{
shift @array1; #removes '.' and '..'. Shift function removes the first element of an array
}
MailNewFiles(@array1);
}
else
{
print "No New Files!";
}
}


sub MailNewFiles
{
use Mail::Mailer;
my $from = "root";
my $to = "root";
my $subject = "Subject: New Files";
my $mailer = Mail::Mailer->new();
$mailer->open({ From => $from,
To => $to,
Subject => $subject,
});
my $header = " New Files";
print $mailer $#_ + 1;#prints the number of new files
print $mailer "$header\n";
foreach(@_)$the files are stored in @_
{
print $mailer "$_\n";
}
}
#end

No comments:

Post a Comment