Doownloading multiple files in multiple directories through FTP with one command

Top Japanese page




Overview

This is not a CGI. This script is executed from a command line. This script allows to download files from multiple directories.

Usage of the command

 getall.pl <username> <password>

The script name is getall.pl. First argument is the username of the FTP site and the second one is password of the FTP site.

Flow

  1. Load Net::FTP
  2. Configure directory information to be downloaded from
  3. Put arguments to variables
  4. Connect to the FTP site and make an object
  5. Login to the FTP site
  6. Perform put command at each directory and download the files
  7. Quit once all uploads are done

A sample code

 use Net::FTP;
 
 my %db = (
          'site' => "ftp.yoursite.com",
          'root' => "myhome/html",
          'local' => 'd:\www\home\html',
          'items' => [qw(
                         index.html
                         bbs/datafile.dat
                         mydata.log
                         )],
       );
 
 die "Usage: $0 <username> <password>\n" if ($#ARGV < 2);
 
 my $ftpsite = $db{site};
 my $username = $ARGV[0];
 my $password = $ARGV[1];
 
 my $ftp = Net::FTP->new("$ftpsite") 
        or die "Cannot connect to $ftpsite : $@";
 $ftp->login("$username", "$password") 
        or die "Cannot login : ", $ftp->message;
 
 foreach my $file (@{$db{items}}){
     $ftp->get("$db{root}/$file","$db{local}/$file") 
        or die "get failed : ", $ftp->message;
     print $ftp->message;
 }
 
 $ftp->quit();

Description of the code

 use Net::FTP;

Load Net::FTP

 my %db = (
          'site' => "ftp.yoursite.com",
          'root' => "myhome/html",
          'local' => 'd:\www\home\html',
          'items' => [qw(
                         index.html
                         bbs/datafile.dat
                         mydata.log
                         )],
       );

FTP site, common directory from root directory, sub-directories to be downloaded from. In this example, the download directories and files will be as follows;

 myhome/html/index.html        --->   d:\www\home\html\index.html
 myhome/html/bbs/datafile.dat  --->   d:\www\home\html\bbs\datafile.dat
 myhome/html/mydata.log        --->   d:\www\home\html\mydata.log
 die "Usage: $0 <username> <password>\n" if ($#ARGV < 2);

If arguments are not correct, exit the script.

 my $ftpsite = $db{site};

Put the FTP server name to a variable.

 my $username = $ARGV[0];
 my $password = $ARGV[1];

Put arguments to variables.

 my $ftp = Net::FTP->new("$ftpsite") 
        or die "Cannot connect to $ftpsite : $@";

Connect to the FTP site. At this point, login has not been done yet.

 $ftp->login("$username", "$password") 
        or die "Cannot login : ", $ftp->message;

Pass username and password here and login. If failed to login, exit with a message.

 foreach my $file (@{$db{items}}){
     $ftp->get("$db{root}/$file","$db{local}/$file") 
        or die "get failed : ", $ftp->message;
     print $ftp->message;
 }

Extract sub-directories one by one and download the files.

 $ftp->quit();

Quit FTP.