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

this should be pretty simple but I can't find the relevant info when I search for it.

I want to copy a whole folder with ftp but can only copy individual files with the code below. How do I put the whole folder?
$ftp->put("foldername");

thanks

edited: Sat Apr 5 21:48:57 2003 by jeffa - title change (was: "moving whole folders")

Replies are listed 'Best First'.
Re: Moving folders using FTP
by phydeauxarff (Priest) on Apr 05, 2003 at 16:26 UTC
    put only moves files, not directories.

    Check out Net::FTP::Recursive

    use Net::FTP::Recursive;
    
        $ftp = Net::FTP::Recursive->new("some.host.name", Debug => 0);
        $ftp->login("anonymous",'me@here.there');
        $ftp->cwd('/pub');
        $ftp->rput( ParseSub => \&yoursub );
        $ftp->quit;
    
Re: Moving folders using FTP
by waswas-fng (Curate) on Apr 05, 2003 at 16:21 UTC
Re: Moving folders using FTP
by zby (Vicar) on Apr 06, 2003 at 19:05 UTC
    An alternative approach (a bit hard to do programmatically) if you have a shell account on the target might be to tar the directory, copy it and untar on the target.
      An alternative approach (a bit hard to do programmatically) if you have a shell account on the target

      Well, this actually is not hard at all to do programmatically; check out this reply to an older thread about simplifying tar transfers between machines. It assumes that ssh is available on both machines, and shows the case were you want to pull data from a remote host to the local host, but other approaches (and the other directionality) are possible.

Re: Moving folders using FTP
by vek (Prior) on Apr 06, 2003 at 17:47 UTC
    You could always FTP each file in the directory which would leave you with the same result - i.e the whole directory contents will have been transferred.
    opendir (DIR, $some_dir) || die "opendir: $!\n"; for my $file_found (readdir DIR) { my $full_path = $some_dir . "/" . $file_found; next if (-d $full_path); $ftp->put($full_path) || die "put: $!\n"; }
    -- vek --

      This would not get any sub-directories. I would replace the calls to opendir,readdir with a call to File::Find


      Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd

        This would not get any sub-directories.

        Right, but I didn't see that as a requirement from the OP.

        I would replace calls to opendir,readdir with a call to File::Find

        This is an interesting point because I've never needed to use File::Find before. Just out of curiosity, why is File::Find better than Perl's own opendir & readdir?

        Update: Should have phrased the question slightly better. I meant to ask why File::Find would be better than opendir & readdir for reading files from a directory bearing in mind that recursing through a directory was not a requirement.

        -- vek --