Quote:
Originally Posted by arunavlp
hi Ganesh,
This wil display all the files listed in the ftp.
but how can i get the latest file ???
Regards,
Arun S
|
You could get them all and sort all that out locally or something like this quick hack (very dependent on output of LIST command)
Code:
#!/usr/bin/perl
use strict;
#use Data::Dumper;
use Net::FTP;
use Date::Parse;
my $site = 'localhost';
my $user = 'anonymous';
my $pass = 'nobody@localhost.com';
my $path = '/';
my $regex = '/\s+JAMA01\.DAT.*$/';
my $debug = 0;
my $ftp = Net::FTP->new($site, Debug => $debug)
or die "Cannot connect to some.host.name: $@";
$ftp->login($user, $pass)
or die "Cannot login ", $ftp->message;
$ftp->cwd($path)
or die "Cannot change working directory ", $ftp->message;
my (@items) = $ftp->dir()
or die "Cannot list directory ", $ftp->message;
my %files;
my $new_file;
my $new_ts=0;
for my $item (@items)
{
next unless($item =~ m/^-/); # only real files
next unless(grep($regex, $item));
my $ts;
my $file;
$item =~ m/\s+(\w+\s+\d+\s+\d+:\d+)\s+(.*)$/;
$ts = $1;
$file = $2;
if($ts)
{
$ts = str2time($ts);
}
else
{
warn("could not parse date on line:\n$item\n");
next;
}
$files{$file}=$ts;
$new_file = $ts > $new_ts ? $file : $new_file;
$new_ts = $ts > $new_ts ? $ts : $new_ts;
}
$ftp->get($new_file)
or die "get failed ", $ftp->message;
$ftp->quit;