Sorry that this is ugly - I wrote it quickly one day...
It checks for the file, and makes sure the date stamp is within 10 minutes.
(Date::Manip was overkill for this). Who knows, I may have stolen this code
from someone else -- nah...too ugly.

#!/usr/bin/perl
use File::stat;
use Date::Manip;

my $maxtime = 10; # in minutes, up to 59; for hours, HHMM... like '1200' for
twelve hours, no minutes
my $name = "/path/to/file/running.txt"; # status file name
my $alert; # alert message holder/indicator if an alert is needed to be sent

if (-e $name) { # does the file exist?
    my $filedate =localtime(stat($name)->mtime); # Get last modified date
    my $err;
    my $delta=&DateCalc($filedate,&ParseDate("now"),\$err); # calc the time
since modified from right now
    $delta =~ s/://g; # strip out separating colons
    $alert = "More than $maxtime minutes since CMS contacted webserver" if
$delta > ($maxtime . '00');
    $alert = "Error: $err" if $err;

} else {
    $alert = "$name doesn't exist";
}

my $status; # = 'OK';
if ($alert) {
  $status = 'Error';
  warn localtime() . " - Status: $status\n";
  &sendEmailAlert($alert);
}

exit 0;

...then elsewhere I had sub sendEmailAlert defined.

Also, I set crontab to run this every 10 minutes.

-----Original Message-----
From: Kevin Struckhoff [mailto:[EMAIL PROTECTED]
Sent: Friday, August 29, 2003 10:24 AM
To: [EMAIL PROTECTED]
Subject: Testing for the existence of a file


Using Perl 5.6.1 on HP-UX.

I need to test for the existence of a file every day. The filename
changes each day because the filename contains a data and time stamp. 
For example, today's filename is KeyLinks_082903_120712.csv. So I would
like to test for KeyLinks*.csv

My code is as follows:

-----

  $file = "$DATA_HOME/KeyLinks\*.csv";
  print $file;
  print "\n";

  if (-e $file) {
     print "File Keylinks*.csv exists!\n";
  } else {
     print "File Keylinks*.csv not understood!\n";
  }

For output, I get:

---
/warehouse/data/KeyLinks*.csv
File Keylinks.csv not understood!
----

It expands the filename and prints it out just fine, but the -e test
fails. Why is that? I have reviewed several reference books and they all
show examples of using the -e test. It mustn't be evaluating the splat
correctly.

Thanks,

Kevin Struckhoff
Data Warehouse Consultant
Yamaha Motors U.S.
[EMAIL PROTECTED]
714-761-7310



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to