Scan/Parse directory for "newest" file, print file timestamp andcompute date/time differen
--0015177409ee055fc1049d827d6e
Content-Type: text/plain; charset=ISO-8859-1
Hi all,
Am wanting to get some advise on how to write this Perl script.
I have a backup directory that I have to check for the existence of file/s
and if the latest file that exist there is 2 days old, that means I have a
problem and had to send an email notification.
To illustrate if, for example, a directory named /backup have the following
files and their timestamps:
/backup/file.01 01-Jan-2011 0500
/backup/file.02 02-Jan-2011 0500
/backup/file.03 03-Jan-2011 0500
.......
.......
/backup/file.31 31-Jan-2011 0500
/backup/file.32 01-Feb-2011 0500
/backup/file.33 02-Feb-2011 0500
I want the script to check the most recent file in a backup directory, i.e.
newest, for example /backup/file.33 and display the timestamp, i.e. date and
time, of the file. Then I want to compare it with today's date and display
the date/time difference in n days n hours n minutes. If the file is more
than 2 days old, that should mean that the backup did not run so I have to
send an email notification.
So for example, if today is 04-Feb-2011 1000 0800 and there is no backup
file created on 03-Feb-2011, when the script runs, it should detect that the
most recent backup file is file.33, "computed" that it is older than 2 days
and then send en email notification.
Sample output from the script should be as below:
===================================================
Checking last backup file on 04-Feb-2011 10:05:
Last backup file = /backup/file.33
Timestamp = 02-Feb-2011 05:00
The file is = 2 days 5 hour/s 5 minute/s old
Send email notification to check backup
===================================================
My main hurdle is the file timestamp and date arithmetic part. Frm
Google'ing, am leaning towards using stat which am hoping will work on both
Unix and Windows.
Some guidance will be much appreciated. Thanks in advance.
--0015177409ee055fc1049d827d6e--
Re: Scan/Parse directory for "newest" file, print file timestampand compute date/time diff
On 11-03-02 11:34 AM, newbie01 perl wrote:
> My main hurdle is the file timestamp and date arithmetic part. Frm
> Google'ing, am leaning towards using stat which am hoping will work on both
> Unix and Windows.
>
> Some guidance will be much appreciated. Thanks in advance.
Use the stat function to get the mtime of the files. The one with the
biggest is the most recent.
Compare it to now and if now > mtime + 2 * 24 * 60 * 60 then output your
message.
Use strftime() from POSIX to change the epoch seconds to human readable
form.
See:
perldoc -f stat
perldoc -f time
perldoc -f localtime
pelrdoc POSIX and search for /strftime/.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Scan/Parse directory for "newest" file, print file timestamp andcompute date/time diff
--0022159763e655ff77049d8303ed
Content-Type: text/plain; charset=ISO-8859-1
First, use File::Find to get your info (or you could use system( ls -l ) and
split - either way). To compare your time stamps, use DateTime and do dt1 -
dt2
--0022159763e655ff77049d8303ed--
Re: Scan/Parse directory for "newest" file, print file timestampand compute date/time diff
On 11-03-02 12:12 PM, shawn wilson wrote:
> First, use File::Find to get your info (or you could use system( ls -l ) and
> split - either way). To compare your time stamps, use DateTime and do dt1 -
> dt2
>
No. If you can do all your calculations using seconds from the epoch,
then do so. Only convert them to human-readable form for the output.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Scan/Parse directory for "newest" file, print file timestamp andcompute date/time diff
On 3/2/11 Wed Mar 2, 2011 8:34 AM, "newbie01 perl"
<newbie01.perl [at] gmail.com> scribbled:
> Hi all,
>
> Am wanting to get some advise on how to write this Perl script.
>
> I have a backup directory that I have to check for the existence of file/s
> and if the latest file that exist there is 2 days old, that means I have a
> problem and had to send an email notification.
>
> To illustrate if, for example, a directory named /backup have the following
> files and their timestamps:
>
> /backup/file.01 01-Jan-2011 0500
> /backup/file.02 02-Jan-2011 0500
> /backup/file.03 03-Jan-2011 0500
> ......
> ......
> /backup/file.31 31-Jan-2011 0500
> /backup/file.32 01-Feb-2011 0500
> /backup/file.33 02-Feb-2011 0500
>
> I want the script to check the most recent file in a backup directory, i.e.
> newest, for example /backup/file.33 and display the timestamp, i.e. date and
> time, of the file. Then I want to compare it with today's date and display
> the date/time difference in n days n hours n minutes. If the file is more
> than 2 days old, that should mean that the backup did not run so I have to
> send an email notification.
>
> So for example, if today is 04-Feb-2011 1000 0800 and there is no backup
> file created on 03-Feb-2011, when the script runs, it should detect that the
> most recent backup file is file.33, "computed" that it is older than 2 days
> and then send en email notification.
The file test operator -M, when applied to a file, will return the age of
the file in days. Thus, you can use a test such as if( -M $filename > 2 ) to
determine if a file is more than 2 days old.
See:
perldoc -f -X
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/