Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   date modified if statement (http://www.velocityreviews.com/forums/t894852-date-modified-if-statement.html)

Naji 10-25-2005 03:18 PM

date modified if statement
 
I'm rather new to Perl and its syntax, and I did try multiple searches
before posting this, but I am using Perl to create a Macro that will
automatically send an e-mail and a specific attachment every time the
attachment is updated. What would the syntax be for the if statement
for code that would send the attachment if it has been modified in the
past 12 hours? Any help would be appreciated. Thanks!


J. Gleixner 10-25-2005 03:23 PM

Re: date modified if statement
 
Naji wrote:
> I'm rather new to Perl and its syntax, and I did try multiple searches
> before posting this, but I am using Perl to create a Macro that will
> automatically send an e-mail and a specific attachment every time the
> attachment is updated. What would the syntax be for the if statement
> for code that would send the attachment if it has been modified in the
> past 12 hours? Any help would be appreciated. Thanks!
>


you_will_find_your_answer() if $you_read_the_documentation_for_stat;

perldoc -f stat

Naji 10-25-2005 03:29 PM

Re: date modified if statement
 
So I searched for documentation for stat which you speak about, I
google'd it and got this:

use File::stat;
$st = stat($file) or die "No $file: $!";
if ( ($st->mode & 0111) && $st->nlink > 1) ) {
print "$file is executable with lotsa links\n";
}
use File::stat qw(:FIELDS);
stat($file) or die "No $file: $!";
if ( ($st_mode & 0111) && $st_nlink > 1) ) {
print "$file is executable with lotsa links\n";
}


I do not think this code is what I'm looking for. Where can I get this
documentation which you speak of? Thanks.


Ian Wilson 10-25-2005 04:33 PM

Re: date modified if statement
 
Dear Google-Groups victim, please quote some context!
http://groups.google.com/googlegroup...html#summarize

Here's how:
http://cfaj.freeshell.org/google/google-groups.html

Naji wrote:
> So I searched for documentation for stat which you speak about, I
> google'd it and got this:
>
> use File::stat;
> $st = stat($file) or die "No $file: $!";
> if ( ($st->mode & 0111) && $st->nlink > 1) ) {
> print "$file is executable with lotsa links\n";
> }
> use File::stat qw(:FIELDS);
> stat($file) or die "No $file: $!";
> if ( ($st_mode & 0111) && $st_nlink > 1) ) {
> print "$file is executable with lotsa links\n";
> }
>
>
> I do not think this code is what I'm looking for.


It is and it isn't.

> Where can I get this
> documentation which you speak of? Thanks.
>


The docs are installed with perl. At a command line type this command:
perldoc -f stat.

Paul Lalli 10-25-2005 04:35 PM

Re: date modified if statement
 
Naji wrote:
> So I searched for documentation for stat which you speak about


Who's "you"? Please provide some context when replying to a Usenet
newsgroup.

> I google'd it and got this:


Google is far from the best way to find documentation for Perl. Use

> use File::stat;
> $st = stat($file) or die "No $file: $!";
> if ( ($st->mode & 0111) && $st->nlink > 1) ) {
> print "$file is executable with lotsa links\n";
> }
> use File::stat qw(:FIELDS);
> stat($file) or die "No $file: $!";
> if ( ($st_mode & 0111) && $st_nlink > 1) ) {
> print "$file is executable with lotsa links\n";
> }
>
> I do not think this code is what I'm looking for. Where can I get this
> documentation which you speak of? Thanks.


Run this command at the command line:
perldoc -f stat
or, for the command-line impaired, use the web interface at:
http://perldoc.perl.org/functions/stat.html

Paul Lalli


wisefamily@integrity.com 10-25-2005 04:42 PM

Re: date modified if statement
 
Naji wrote:
> I'm rather new to Perl and its syntax, and I did try multiple searches
> before posting this, but I am using Perl to create a Macro that will
> automatically send an e-mail and a specific attachment every time the
> attachment is updated. What would the syntax be for the if statement
> for code that would send the attachment if it has been modified in the
> past 12 hours? Any help would be appreciated. Thanks!


I think that syntax would be like this (assuming you have the
attachment name in the variable $attachment):

if(-M $attachment <= 0.5) {
# Attachment was modified in the last 12 hours
}

The part "-M $attachment" is a file test. File tests are made up
of a dash, followed by a letter, and then the name of the file. The
"-M" file test returns the number of days since the file was last
modified (as of the script starting time). 12 hours is 0.5 days, so we
check to see if the modification time is less than or equal to 0.5.
The code sending the attachment would replace the comment inside.

Also, as J. Gleixner wrote, you can use the stat function like this:

($device, $inode, $mode, $numOfHardLinks, $userId, $groupId,
$deviceIdentifier, $size, $lastAccessed, $lastModified,
$inodeLastChanged) = stat($attachment);

We are interested in the $lastModified variable which would the last
modification time for $attachment, in the number of seconds since the
epoch. Using the stat function, you could also test the modification
time this way:

$currentTime = time();
($device, $inode, $mode, $numOfHardLinks, $userId, $groupId,
$deviceIdentifier, $size, $lastAccessed, $lastModified,
$inodeLastChanged) = stat($attachment);

if($lastModified + 43200 >= $currentTime) { # 43200 is the number of
seconds in 12 hours.
# Attachment was modified in the last 12 hours
}

Hope this helps,
David


Mothra 10-25-2005 04:45 PM

Re: date modified if statement
 
Naji wrote:
> I'm rather new to Perl and its syntax, and I did try multiple searches
> before posting this, but I am using Perl to create a Macro that will
> automatically send an e-mail and a specific attachment every time the
> attachment is updated. What would the syntax be for the if statement
> for code that would send the attachment if it has been modified in the
> past 12 hours? Any help would be appreciated. Thanks!


This might help.

use strict;
use warnings;
use DateTime;
my $file = 'your_file_here';
my $dt1 = DateTime->now;
my $dt2 = $dt1 - DateTime::Duration->new( hours => 12 );
unless ( -e $file ) {
die "$file does not exists\n";
}
my $dt_file = DateTime->from_epoch( epoch => ( stat($file) )[9] );
#make sure $dt1 is less that $dt2
( $dt1, $dt2 ) = ( $dt2, $dt1 ) if $dt1 > $dt2;

if ( $dt1 <= $dt_file and $dt_file <= $dt2 ) {
print "yep! we need to mail the file";
}

I hope this helps

Mothra



Naji 10-25-2005 04:55 PM

Re: date modified if statement
 
I appreciate the help. I did find the online stat documentation,
however I am a Perl newbie and need to grasp some of the other
concepts. What would the If Statement look like if I'm trying to test
for files that have been modified in the last 24 hours? I will go
through some tutorials and try to figure it out on my own, but any help
or direction would be much appreciated.


Naji 10-25-2005 05:02 PM

Re: date modified if statement
 
Thank you for your help! I appreciate it a lot!!!!

Mothra wrote:
> Naji wrote:
> > I'm rather new to Perl and its syntax, and I did try multiple searches
> > before posting this, but I am using Perl to create a Macro that will
> > automatically send an e-mail and a specific attachment every time the
> > attachment is updated. What would the syntax be for the if statement
> > for code that would send the attachment if it has been modified in the
> > past 12 hours? Any help would be appreciated. Thanks!

>
> This might help.
>
> use strict;
> use warnings;
> use DateTime;
> my $file = 'your_file_here';
> my $dt1 = DateTime->now;
> my $dt2 = $dt1 - DateTime::Duration->new( hours => 12 );
> unless ( -e $file ) {
> die "$file does not exists\n";
> }
> my $dt_file = DateTime->from_epoch( epoch => ( stat($file) )[9] );
> #make sure $dt1 is less that $dt2
> ( $dt1, $dt2 ) = ( $dt2, $dt1 ) if $dt1 > $dt2;
>
> if ( $dt1 <= $dt_file and $dt_file <= $dt2 ) {
> print "yep! we need to mail the file";
> }
>
> I hope this helps
>
> Mothra



A. Sinan Unur 10-25-2005 08:55 PM

Re: date modified if statement
 
Purl Gurl <purlgurl@purlgurl.net> wrote in news:435E68F3.8080800
@purlgurl.net:

> In the future, work at writing articles which are clear,
> concise and coherent so as not to leave readers guessing.
>
> Purl Gurl


Please read the posting guidelines for this group.

> #!perl


use strict;
use warnings;

missing.

> opendir (DIRECTORY, "c:/apache/users/test/");


You never check the whether this call succeeded:

opendir my $dir, 'c:/apache/users/test/'
or die "Cannot opendir c:/apache/users/test/: $!";

> while (defined ($filename = readdir (DIRECTORY)))
> {
> if ( ($^T - 43200) <= (stat ($filename)) [9])


You should read the documents for readdir. Since you haven't changed
directory to c:/apache/users/test/, the stat call is wrong.

> { push (@Array, $filename); }
> }
> if (scalar (@Array) > 0)


No need for scalar here:

if (@Array)

works just as well.

Here is a better way (in the sense that it is correct, easier to
understand etc):

#!/usr/bin/perl

use strict;
use warnings;

my $src_dir = $ARGV[0] || '.';

opendir my $dir, $src_dir or die "Cannot opendir '$src_dir': $!";

my @files_to_email = grep {
plain_files_modified_in_last_12_hours($src_dir, $_)
} readdir $dir;

closedir $dir;

{
local $" = "\n";
print "@files_to_email\n";
}

use File::Spec::Functions 'catfile';
sub plain_files_modified_in_last_12_hours {
my ($dir, $file) = @_;
return if $file =~ m{ \A \.{1,2} \z }xms;
my $path = catfile $dir, $file;
return unless -f $path;
return unless (time - (stat $path)[9]) <= 12 * 60 * 60;
return $path;
}

__END__


All times are GMT. The time now is 01:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.