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;

In reply to Net::FTP downloading multiple files from multiple directories by jonnyfolk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.