Madhan
Madhan

Reputation: 1321

How to download a folder/directory from the FTP server using perl?

I have an array of folders in the FTP serever. For an example,

@ftp_dirs=('/Tarun/Netdomais','/Tarun/Testing','/Tarun/Tested_files')

I need to download each folders in the array from the FTP server to local folder (c:\ftp_downloaded).

I have written the below lines

use strict;
use Net::FTP;


my $ftp=Net::FTP->new("hostname",Debug=>0);
$ftp->login("user_name","password");
$ftp->cwd("/Tarun");

my @ftp_dirs=('/Tarun/Netdomais','/Tarun/Testing','/Tarun/Tested_files');
my $local='c:\ftp_downloaded';

foreach my $ftp_folder(@ftp_dirs){
  $ftp->get($ftp_folder,$local);
}

The above code is not working. Because the get method is only applicable for downloading files from the ftp not folders.

Is it possible to download a folder from the ftp?

Upvotes: 1

Views: 7033

Answers (2)

snoofkin
snoofkin

Reputation: 8895

Just use Net::FTP::Recursive.

Example:

use Net::FTP::Recursive;

$ftp = Net::FTP::Recursive->new("some.host.name", Debug => 0);
$ftp->login("anonymous",'[email protected]');
$ftp->cwd('/pub');
$ftp->rget( ParseSub => \&yoursub );
$ftp->quit;

Upvotes: 4

Tudor Constantin
Tudor Constantin

Reputation: 26861

First of all, start all your perl scripts with:

use strict;
use warnings;

Second, you miss a ; after line:

my @ftp_dirs=('/Tarun/Netdomais','/Tarun/Testing','/Tarun/Tested_files')

Third, I think you could try to use the command method inherited by Net::FTP from Net::Cmd and issue a ftp mget command, or emulate the mget with something like:

$ftp->get($_) for grep { 1 } $ftp->ls;

Upvotes: 5

Related Questions