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

Hi Guys,

I've spent certain amount of hours searching for a solution and I couldn't find it.:(
Can you guys help me? I'm sure it's a easy do and that I can't see the forrest from all the trees.

I have a perl script with which I daily download files from a remote FTP server.
$ftp = Net::FTP->new($host, Debug => 0, Passive => 1); $ftp->login ($username, $password); $ftp->binary(); @set_of_files = $ftp->ls("*$thismonth-$thisday-$thisyear*.tar" +);
etc.
Is it possible with $ftp->get to check if a file already exists on a local folder before downloading it from ftp?

Thank you and best regards,


Mario

Replies are listed 'Best First'.
Re: Net::FTP - Check if file already exists locally before download
by hippo (Archbishop) on Feb 12, 2019 at 10:52 UTC

    Is this what you are looking for?

    for my $file (@set_of_files) { $ftp->get ($file) unless -e $file; }
      Thanks! Exactly what I thought, couldn't see the easiest solution.

      Hi Monks,

      I got a follow up question. Is it possible to use the same command to check if file exists in two or more different directorites?

      for my $file (@set_of_files) { $ftp->get ($file) unless -e /dir1/$file; }

      so basically to check:

      unless -e /dir1/$file unless -e /dir2/$file

      something like that?

      Thanks again.

        for my $file (@set_of_files) { $ftp->get ($file) unless (-e "/dir1/$file" or -e "/dir2/$file"); }