SPUG:checking for newer file on FTP server

Jeremy Kahn kahn at cpan.org
Fri May 9 17:41:02 CDT 2003


Al Garay wrote:

>I can see how to use NET::FTP to connect and get the
>file. I'm not sure how to do the comparison without
>first downloading the file ... I can store a MD5
>checksum or use the size of file or date to do the
>comparison.
>
The documentation from Net::FTP (`perldoc Net::FTP`) says:

> mdtm ( FILE )
>     Returns the /modification time/ of the given file
>
> size ( FILE )
>     Returns the size in bytes for the given file as stored on the
>     remote server.
>
>     *NOTE*: The size reported is the size of the stored file on the
>     remote server. If the file is subsequently transfered from the
>     server in ASCII mode and the remote server and local machine have
>     different ideas about "End Of Line" then the size of file on the
>     local machine after transfer may be different
>
I would suggest using one or both of these (before you download the 
file). Combined with the local timestamp you should be able to tell 
which file needs to be downloaded.

(`perldoc -q timestamp` yields):

> Found in /usr/lib/perl5/5.8.0/pod/perlfaq5.pod
>   How do I get a file's timestamp in perl?
>             If you want to retrieve the time at which the file was last
>             read, written, or had its meta-data (owner, etc) changed, you
>             use the -M, -A, or -C file test operations as documented in
>             perlfunc. These retrieve the age of the file (measured against
>             the start-time of your program) in days as a floating point
>             number. Some platforms may not have all of these times. See
>             perlport for details. To retrieve the "raw" time in seconds
>             since the epoch, you would call the stat function, then use
>             localtime(), gmtime(), or POSIX::strftime() to convert 
> this into
>             human-readable form.
>
>             Here's an example:
>
>                 $write_secs = (stat($file))[9];
>                 printf "file %s updated at %s\n", $file,
>                     scalar localtime($write_secs);
>
>             If you prefer something more legible, use the File::stat 
> module
>             (part of the standard distribution in version 5.004 and 
> later):
>
>                 # error checking left as an exercise for reader.
>                 use File::stat;
>                 use Time::localtime;
>                 $date_string = ctime(stat($file)->mtime);
>                 print "file $file updated at $date_string\n";
>
>             The POSIX::strftime() approach has the benefit of being, in
>             theory, independent of the current locale. See perllocale for
>             details.

[snipped rest of perldoc -q results]

Hope that helps,

--jeremy




More information about the spug-list mailing list