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

Hi Monks!

I am downloading files using Net::FTP to my local directory, it works just fine, but I am trying to only get NEW files from my FTP server, if a file is already in to the "$path" ignore the file ( dont overwrite it), in another words don't download this file. That way I will only have new files downloaded from my FTP server and any previously downloaded files will not get over written, is that a way I could do this right here in this code?
Thanks for the Help!!!

my $host="myserver"; my $dir = "/home/ftp/files/"; my $login = "xyz"; my $pw = "XYZ"; my $name; my $path = "c:/files/"; my $ftp = Net::FTP->new($host) or die "Cannot connect to $host: $@"; $ftp->login($login,$pw) or die "Cannot login to $host as $login: $@"; $ftp->cwd($dir) or die "Cannot change working directory: $@"; my @files=$ftp->ls or die "Cannot list current directory: $@"; foreach my $x_files(@files) { $ftp->get($x_files, "$path".$x_files) or die "Can't fetch $x_files: $!\n"; }

Replies are listed 'Best First'.
Re: FTP - Downloading New Files Only
by apl (Monsignor) on Mar 10, 2008 at 19:46 UTC
    if ( -f $path.$x_files ) { print "File $x_files already exists!\n"; } else { $ftp->get($x_files, "$path".$x_files) or die "Can't fetch $x_f +iles: $!\n"; }
      My copy of the Camel Book used -f, and so do I. 8-)

      You are, of course, correct. -e would work as well.

      Should this line if ( -f $path.$x_files ) be if ( -e $path.$x_files ) with "-e"?