CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
FTP files from multiple source directories to multiple target directories

 



Spence32
New User

Sep 26, 2008, 11:43 AM

Post #1 of 5 (899 views)
FTP files from multiple source directories to multiple target directories Can't Post

Hi,

I am looking to create a Perl script to FTP files from multiple source directories to multiple target directories, with each source directory is mapped to a particular target directory. For example, FTP all files from source dir 100 to target dir 100, source 200 to target 200, etc.

Here is a script I am working from for a single file:

use Net::FTP;

chdir "/tmp";
$ftp = Net::FTP->new("host", Debug => 0);
$ftp->login("username",'-password@');
$ftp->cwd("/dir");
$ftp->get("the.file");
$ftp->quit;

I know I need to use a loop, but I am not quite sure of how to use it without defining multiple variables for each directory. Any suggestions?

Thanks


shawnhcorey
Enthusiast


Sep 26, 2008, 5:48 PM

Post #2 of 5 (894 views)
Re: [Spence32] FTP files from multiple source directories to multiple target directories [In reply to] Can't Post


In Reply To
Hi,

I am looking to create a Perl script to FTP files from multiple source directories to multiple target directories, with each source directory is mapped to a particular target directory. For example, FTP all files from source dir 100 to target dir 100, source 200 to target 200, etc.

Here is a script I am working from for a single file:

use Net::FTP;

chdir "/tmp";
$ftp = Net::FTP->new("host", Debug => 0);
$ftp->login("username",'-password@');
$ftp->cwd("/dir");
$ftp->get("the.file");
$ftp->quit;

I know I need to use a loop, but I am not quite sure of how to use it without defining multiple variables for each directory. Any suggestions?

Thanks


In you want just the files in one directory, use glob (see `perldoc -f glob`). If you want the subdirectories too, use File::Find (see `perldoc File::Find`).

Something like this:

Code
for my $file ( glob( $dir ) ){ 
next unless -f $file; # skip all but regular files

# ftp the file
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Spence32
New User

Sep 29, 2008, 5:17 AM

Post #3 of 5 (873 views)
Re: [shawnhcorey] FTP files from multiple source directories to multiple target directories [In reply to] Can't Post

Thanks for the reply.

But what about if all directories on the source are at the same level, and all the directories on the target are as well. So

Source Dir 100 --> Target Dir 100
Source Dir 200 --> Target Dir 200
Source Dir 300 --> Target Dir 300
Source Dir 400 --> Target Dir 400
Source Dir 500 --> Target Dir 500

with no sub directories.

Thanks


shawnhcorey
Enthusiast


Sep 29, 2008, 6:11 AM

Post #4 of 5 (867 views)
Re: [Spence32] FTP files from multiple source directories to multiple target directories [In reply to] Can't Post


In Reply To
Thanks for the reply.

But what about if all directories on the source are at the same level, and all the directories on the target are as well. So

Source Dir 100 --> Target Dir 100
Source Dir 200 --> Target Dir 200
Source Dir 300 --> Target Dir 300
Source Dir 400 --> Target Dir 400
Source Dir 500 --> Target Dir 500

with no sub directories.

Thanks



Code
my @dirs = ( 
[ 'Source Dir 100' => 'Target Dir 100' ],
[ 'Source Dir 200' => 'Target Dir 200' ],
[ 'Source Dir 300' => 'Target Dir 300' ],
[ 'Source Dir 400' => 'Target Dir 400' ],
[ 'Source Dir 500' => 'Target Dir 500' ],
);

for my $dir_pair ( @dirs ){
my $src_dir = $dir_pair->[0];
my $trgt_dir = $dir_pair->[1];

chdir $src_dir;
$ftp->cwd( $trgt_dir );

for my $file ( glob( '*' ) ){
next unless -f $file;
$ftp->put( $file );
}
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Spence32
New User

Sep 29, 2008, 7:23 AM

Post #5 of 5 (862 views)
Re: [shawnhcorey] FTP files from multiple source directories to multiple target directories [In reply to] Can't Post

Thanks again, I can see how it's done now.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives