2007/10/5, Paul Lalli <[EMAIL PROTECTED]>:
> On Oct 5, 7:50 am, [EMAIL PROTECTED] (Jeff Pang) wrote:
> > 2007/10/5, [EMAIL PROTECTED]
> > <[EMAIL PROTECTED]>:
> >
> > > I would like to compare two files by comparing the files dates.
> > > If one file shows
> > > ls -la May 12 2003 filename
> > > and the other name shows the same date they are OK for me (I'm not
> > > interested in the time part only the date of the file)
> > > But if the dates are not the same I would like to copy one of the
> > > files.
> > > How do I do this in Perl?
>
> > if ( int(-M "file1.txt") != int(-M "file2.txt") ) {
> >     # copy the file
> > }
>
> That's a very naïve approach that will frequently fail.
> For example:
> File one modified 10/1/2007 9am
> File two modified 10/1/2007 3pm
> and the current time is 10/2/2007 12pm
>
> -M 'file1' will return 1.125
> -M 'file2' will return 0.875.
>
> int(1.25) == 1
> int(0.875) == 0

You're right here.


> sub m_to_date {
>    my $days_ago = shift;
>    my $ts = time() - ($days_ago * 24 * 60 * 60);
>    my $date = strftime('%Y-%m-%d', localtime($ts));
>    return $date;
> }

-M is not needed here.to get the date,one can just stat it,

sub get_date {
    my $file = shift;
    my $mtime = (stat $file)[9];
    return strftime '%Y-%m-%d', localtime($mtime);
}


-- 
Jeff Pang - [EMAIL PROTECTED]
http://www.rwweb.co.cc

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to