Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comparing file dates then downloading newer files through ftp

by linebacker (Scribe)
on Jun 25, 2002 at 20:44 UTC ( #177209=perlquestion: print w/replies, xml ) Need Help??

linebacker has asked for the wisdom of the Perl Monks concerning the following question:

Using Net::FTP as my network transport component, I have a situation where I will have a file on my local machine. Call it 0613x86.exe. I go to an ftp site and want to figure out if there is a newer file (perhaps 0615x86.exe). While this sounds easy, I am having a hell of a time getting the mmdd (Filename) out of the arrays and files, then doing the comparison as far as which is the newer file. I have had some help getting this far and feel close but just need a little extra code. Thanks very much, I am trying hard to learn Perl on my own, but I am really struggling with files, arrays, regex, and how Perl wants those items expressed.
#!/usr/local/bin/perl -w use Net::FTP; $host = 'www.ftp.com'; $user = 'anonymous'; $pass = 'updatepuller@ftp.com'; $remote_dir = '/path/to/remote/filesendinginmmddx86.exe'; $oldfilename = `ls current-sig`; chomp $oldfilename; $destination_dir= "current-sig"; $ftp = Net::FTP->new($host, Debug => 1); $ftp->login($user,$pass); my @listing = $ftp->ls("$remote_dir"); for (@listing) { if ( /x86\.exe/i ) { open(FH, ">> myfile.txt") || die "Can't open $file for write: $!"; print FH "$_\n" } } close FH; #$ftp->get("$remote_dir/$filename","$destination_dir/$filename"); $ftp->pasv(); $ftp->quit; open( OUT, "> myfile.new") or die "$!"; open( NFH, "myfile.txt") or die( "Cannot open file : $!" ) ; while (<NFH>) { #($month,$day) =~ /(\d\d)(\d\d)x86\.exe/ # m/([0-9][0-9][0-9][0-9])/; print( OUT "$1\n" ); } #close NFH or die( "Cannot close file: $!" ); #close OUT or die( "Cannot close file: $!" ); #open( NEWTIMES, "<myfile.new") or die "$!"; #while ( <NEWTIMES> ) { # m/([0-9][0-9])/; # @month = ("$1\n") ; # } #while ( <NEWTIMES> ) { # s/([0-9][0-9])//; # @day = ("$1\n") ; # } #close NEWTIMES or die( "Cannot close file: $!" );

Edit by dws to clean up title

Replies are listed 'Best First'.
Re: Comparing file dates then downloading newer files through ftp
by grinder (Bishop) on Jun 25, 2002 at 22:22 UTC
    You're automating the download of new virus definitions from Symantec, right? I have code to do this at work, but due to a misplaced fit of paranoia the other day, I changed my password on my ssh gateway and so I'm locked out right now. I can post you my code tomorrow, but in the meantime here a few hints to get you started.

    The main problem is dealing with the end-of-year wrap around, when file 0112x86.exe is more recent than 1221x26.exe. Another problem is that sometimes the filename lies. The date encoded in the filename does not agree with the filesystem time. I choose to trust the filesystem time. The following code (which I am writing from memory), will determine the most recent file on the remote server that follows the filename convention.

    #! /usr/local/bin/perl -w use strict; use Net::FTP; my $host = 'ftp.example.com'; my $user = 'anonymous'; my $pass = 'grinder@localhost'; my $remote = '/pub'; my $ftp = Net::FTP->new($host); # error $ftp->login($user,$pass); # checking my @listing = $ftp->dir($remote); # omitted my $latest_timestamp = 0; my $latest = undef; my $cur_year = (localtime)[5] + 1900; for my $line( @listing ) { my( $prot, $links, $user, $group, $size, $month, $day, $yeartime, $e +ntry ) = ($line =~ /^(\S+)\s+(\d+)\s+(\w+)\s+(\w+)\s+(\d+)\s+(\w+)\s+(\S+)\s+ +(.*)$/); if( $entry =~ /^(\d\d)(\d\d)i86\.exe$/ ) { my( $year, $hour, $min ) = do { $yeartime =~ /^(\d\d):(\d\d)$/ ? ( $cur_year, $1, $2 ) : ( $yeartime, 0, 0 ) }; my $timestamp = sprintf '%04d%02d%02d%02d%02d', $year, $month, $day, $hour, $min; if( $latest_timestamp lt $timestamp ) { $latest_timestamp = $timestamp; $latest = $entry; } } } exit unless $latest_timestamp; my $prev = undef; if( open IN, 'latest' ) { $prev = <IN>; close IN; chomp $prev; } if( !defined($prev) or $prev lt $latest_timestamp ) { $ftp->get("$remote/$entry"); open OUT '>latest' or die "latest for output: $!\n"; print "$latest_timestamp\n"; close OUT; }

    Basically, you scan the remote directory, find the most recently updated file, and see if it is more recent than the most recent timestamp you have cached locally. If it is, get that file, and update your local timestamp.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Comparing file dates then downloading newer files through ftp
by kvale (Monsignor) on Jun 25, 2002 at 22:24 UTC
    One problem you could be having with your date-extraction regexp is case sensitivity and using =~ (matching) instead of = (assignment). Try
    ($month,$day) = /(\d\d)(\d\d)x86\.exe/i;
    -Mark

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://177209]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2019-10-23 21:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?