filedatetimeperltimestamp

How do I copy the latest from ftp in a perl script


I have a perl script which copies one file from a directory on an ftp server. The filename has the form FPZB01_00000118.txt. The number before .txt is incremented by 1 every day. There should be only one file on the remote server each day. If there are more than 1 files, the script will copies all the files, which is not desired. I am trying to add code to copy only today's file, but am stuck. The code is below, leaving out use::strict, ... and other definitions.

my $ftp = Net::FTP->new( $host, Timeout => 360, Passive => 1, Debug=>1 ) or die "Error connecting to $host: $!\n";
$ftp->binary();
$ftp->login($User,$Pass) or die "Cannot login to $User\n";
$ftp->cwd($remote_dir) or die "Could not change remote working directory to  $remote_dir\n";
my @remote_files = $ftp->ls($glob);

Added code

my @file2copy = grep /^FPZB01/, $ftp->ls();
foreach my $fl (@file2copy){
 my $mdtime = $ftp->mdtm($fl);
 print $f "modtime $mdtime\n";
 exit;
}

End Added code

foreach my $file (@remote_files) {

 # Transfer file
 print "Getting $file\n" if $debug;
 eval {$ftp->get($file, "$local_dir/$file") or print("Can't send file $file\n")};
 if ($@ =~ /Timeout/){
  print "Got a timeout Issue: $@";
 }     
}

#   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print "copying ends\n";
 exit 0;

I have tried to print the modification print $f "modtime $mdtime\n"; This is what I get:

modtime 1697627482. 

No idea what this means. Assistance will appreciated on how to use $ftp->mdtm($fl)


Solution

  • Just sort the list of files and transfer the last one in the array.

    # Use grep to ignore any files with the wrong name
    my @remote_files = grep { /^FPZB/ } sort $ftp->ls($glob);
    
    my $file_to_transfer = $remote_files[-1];