Operating System - HP-UX
1820645 Members
2009 Online
109626 Solutions
New Discussion

Re: How to get files from dir using use Net::FTP module in perl

 
diwakar_4
Frequent Advisor

How to get files from dir using use Net::FTP module in perl

Hi All,

I have written code to get all ZIP files from FTP server and put in Unix.

But i am not able to navigate from one dir to other.

FTP Server(Windows) Dirstrucre is :

/root/';
/root/dir1/:
Z1.zip,z11.zip ..

/root/dir2/:
Z1.zip,z11.zip ..

Now i need to get all files one by one.
$dirpath = '/root/';
$ftp = Net::FTP -> new('X.X.X.X', Port => 21, Debug => 0);
$ftp->login('xxx','0000');
$ftp->binary();
$ftp->cwd($dirpath) or die "Can Not Change Dir";
foreach my $name ('/root/dir1', '/root/dir2') {
$ftp->cwd($name) or die "Can not change the dir";
foreach my $file($ftp->ls)
{
next unless $file=~/.*\.zip/;
$ftp->get($file);
printf("Sucessfully copied files");
}
}

It is not able to change the dirs in FTP server. Can some one please tell what is the problem.

Thanks
1 REPLY 1
James R. Ferguson
Acclaimed Contributor

Re: How to get files from dir using use Net::FTP module in perl

Hi:

If the directory doesn't exist on the _remote_ server, the $ftp->cwd()' will fail.


Try this test case:

# ./myftp
#!/usr/bin/perl
use strict;
use warnings;
use Net::FTP;
my $ftp = Net::FTP->new('x.x.x.x', Port=>21, Debug=>1) or die "Open failed\n";
$ftp->login('acct','pass') or die "Login failed\n";
for my $path qw( /tmp /var /dummydir ) {
$ftp->cwd($path) or warn "failed cd to '$path'\n", $ftp->message, "\n";
}

...

Regards!

...JRF...