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

I posted yesterday and thanks to the help I received I was able to retrieve a file from a directory on another server into the directory specified on the home server.

I have been trying to expand this idea in order to download files from multiple directories within the starting directory. However I have run into a problem that the script locates the correct file in the directory, but it dies on the 'get' command with message "Couldn't get $filename - 1". Using the simpler script of yesterday the file downloads perfectly, and I have compared the paths created in the two scripts and they seem to be the same. Yet the simple one works but not the other...

I wonder if someone can let me know what might be causing this

The code I wrote yesterday (works):

my $home="mysite.com"; my $username="username"; my $password="password"; my $directory="data/edit"; my $filename="data.txt"; my $filename1="path/to/file/data.txt"; my $ftp = Net::FTP->new("$home") or die "Can't connect: 1 $@\n"; $ftp->login($username, $password) or die "Couldn't login - 1\n"; $ftp->cwd($directory) or die "Couldn't change direct +ory - 1\n"; $ftp->get($filename, $filename1) or die "Couldn't g +et $filename - 1\n"; my @lines = $ftp->ls("/data/edit"); $ftp->quit;

The following is the code to get the directories and write the files (dies):

my $home="mysite.com"; my $username="username"; my $password="password"; my $directory="/data"; my $dirname ="path/to/dir"; my $ftp = Net::FTP->new("$home") or die "Can't connect: 1 $@\n"; $ftp->login($username, $password) or die "Couldn't login - 1\n"; $ftp->cwd($directory) or die "Couldn't change direct +ory - 1\n"; my @lines = $ftp->ls("/data"); foreach (@lines) { my $directory1 = "$directory/$_"; my $homedir = "$dirname/$_"; unless ( /back_up|contact/) { my @lines1 = $ftp->ls($directory1); foreach my $filename (@lines1) { print "<br>directory1: $directory1<br>"; print "<br>homedir: $homedir<br>"; my $filename1 = "$homedir/$filename"; $ftp->get($filename, $filename1) or die "Couldn't get $fil +ename - 1\n"; } } } $ftp->quit;

Replies are listed 'Best First'.
Re: Net::FTP downloading multiple files from multiple directories
by valdez (Monsignor) on Oct 29, 2003 at 17:57 UTC

    You may want to turn on Debug and use methods provided by Net::Cmd (from which Net::FTP inherits) to discover what is going wrong.

    Ciao, Valerio

Re: Net::FTP downloading multiple files from multiple directories
by castaway (Parson) on Oct 30, 2003 at 06:33 UTC
    You're doing a $ftp->cwd($directory) at the beginning, and then in your loop, you're looking in "$directory/$_", which means you're looking at /data/data/<filename>, instead of /data/<filename>, are you sure that was what you meant to do?

    My guess is that you want to remove the first $ftp->cwd($directory). And maybe check if that @lines1 and the loop following it actually contain/do anything.

    C.