Ftp code in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ftp code in Perl
# 8  
Old 03-05-2007
Wildcard is typically implemented by FTP clients, by doing an ls and then expanding the glob itself to arrive at the final list of matching files. The FTP protocol specification does not specify wildcards at all, so it is not surprising the module doesn't support it. You will need to do your own processing with ls.
# 9  
Old 03-05-2007
Do u mean using ls in this perl module ?
Can you show me an example ?
# 10  
Old 03-05-2007
If you look at the documentation:
Quote:
ls ( [ DIR ] )

Get a directory listing of DIR, or the current directory.

In an array context, returns a list of lines returned from the server. In a scalar context, returns a reference to a list.
my @lines = $ftp->ls();

@lines will contain a list of entries for that dir, including '.' and '..' and all hidden '.xxxxx' files and dir. You may not wish to include them. Just filter them as you need for your circumstances.

There are many ways to filter the content of an array, but I will show you my favourite way (you will see I use a lot on this forum). For instance, this is to exclude all entries starting with a dot, and getting the rest over FTP in a loop (untested):
Code:
foreach my $file ( map { (/^[^\.].+$/) ? $_ : () } @lines ) {
     $ftp->get($file);
}

By the way, I just found a wrapper to Net::FTP (called Net::FTP::Simple) that supports retrieving a set of files based on a regular expression (look for file_filter in the doc). It uses Net::FTP as the core. This module is not a builtin module. You need to install it separately if you want to use it. Again, I have no experience with it as I just saw it by coincidence.

http://search.cpan.org/~wilco/Net-FT.../FTP/Simple.pm
# 11  
Old 09-05-2008
Hi cbkihong,

I actually followed yr code but in the end i didn;t get to ftp anything. Is the ftp session too fast, because the number of files i need to ftp is like close to 100 files.
Can you give me some advice ?

Code:
#!/usr/local/bin/perl

use Net::FTP;

foreach $t ( 10.10.10.10   20.20.20.20 ) {
                chdir "$script_dir";
                print "Now Processing $t ...\n";

                $ftp = Net::FTP->new("$t", Timeout=>240, Debug => 0)
                or die "Cannot connect to some.host.name: $@";

                $ftp->login("userid",'password')
                or die "Cannot login ", $ftp->message;

                $ftp->cwd("/myreports")
                or die "Cannot change working directory ", $ftp->message;

                my @lines = $ftp->ls(<*sorts>);
                        foreach my $summary_names ( map { (/^[^\.].+$/) ? $_ : () } @lines ) {
                                $ftp->get($summary_names);
                        };

                $ftp->quit;
        };

# 12  
Old 09-05-2008
I think <*sorts> is getting globbed locally. Try just putting it in single quotes; you don't want it to be evaluated at all, just sent to the remote server.
# 13  
Old 09-05-2008
Hi Era,

I think the below code works.
But it seems that the code tries to ftp everything in the folder including file names like AAA_sorts_inter , BBB_sorts_inter and so on.
My required files to be ftped over should only be of these names like AAA_sorts, BBB_sorts

Code:
 $ftp->ls(<*sorts>);


Last edited by Raynon; 09-05-2008 at 05:55 AM..
# 14  
Old 09-05-2008
Since you are filtering the list anyway, why not filter it down to just the ones you want.

Code:
my @lines = grep { /^([^.].*)?sorts$/ } $ftp->ls();
foreach my $name (@lines) {
    $ftp->get($name);
}

(Beats me why cbkihong suggested to use map here; grep would seem to me like the correct operator.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to ftp the files in perl.?

i want to upload two different files in to two different directories(path) on ftp server.but i am able to ftping only one at a time :(.. not both simultaneously. here i have connectd the ftp and use the binary mode to transfer the files $ftp = Net::FTP->new($ftphost, Debug => 0) or... (2 Replies)
Discussion started by: aarts
2 Replies

2. Shell Programming and Scripting

FTP from one directory to another using perl

Hi All I am stuck with a problem and i want your help, I have two directories dir1 and dir2 The files present in dir1 is a1,a2 a3 a4 What i want to is to FTP the files present in the dir1 to dir2 (with .txt extension at the end.) with the help of the Perl. The output expected is The... (12 Replies)
Discussion started by: parthmittal2007
12 Replies

3. HP-UX

[Solved] Unable to rename file in ftp server .Net:FTP perl

Hello All, I am trying to connect to ftp server and get the files. Also i need to rename the file in other ftp dir. rename method is not allowing me to rename the file in other dir. When i tried copy command by using net::FTP:FILE then perl says it is not installed. Can some body help me to... (2 Replies)
Discussion started by: krsnadasa
2 Replies

4. Shell Programming and Scripting

ftp in perl

Hi, I have three files in this /home/mani/ location. I would like to ftp to another server. could you please give perl for that requirement. Thanks, Mani (10 Replies)
Discussion started by: Mani_apr08
10 Replies

5. Shell Programming and Scripting

Perl FTP navigation

Hi Experts, I have this requirement to list dirs and files of an FTP server on regular basis. I was able to do it by following script: $ftpobj = Net::FTP -> new ("$ftpsrv") || die "Cannot connect to FTP $ftpsrv"; $ftpobj -> login("user","passwd"); $ftpobj -> cwd ("/root_dir"); @rootdir... (1 Reply)
Discussion started by: mtomar
1 Replies

6. Shell Programming and Scripting

script FTP in perl

this script should search directories read,,search for file for daily reports. you must provide the beginning day, ending day and month. this script will pull files for days and month specified and Ftp them to another server. Thanks for any help you provide (2 Replies)
Discussion started by: lemseffert
2 Replies

7. Shell Programming and Scripting

Net::Ftp in perl

I am trying to execute a script in another server, i used Net::Ftp module How to execute unix command in another server by using Net::Ftp module.. #!/usr/bin.perl ### Perl script to $ftp->login($user_name,'password') or die "Cannot login ", $ftp->message;... (2 Replies)
Discussion started by: pritish.sas
2 Replies

8. Shell Programming and Scripting

How to automate ftp in perl

My situations is I cannot use NET::ftp. So I need to have a way to automate ftp. I know how to do it in ksh: #!/usr/bin/ksh ftp -i -v -n $host_name <<_FTP >> $log_file 2>&1 user $user_name lcd $local_dir cd $remote_dir put $file_name bye _FTP But how can I do it in perl? Note:... (6 Replies)
Discussion started by: egyfan
6 Replies

9. Shell Programming and Scripting

using perl to ftp

Hi all, I am trying to download a build from an ftp server. My problem is that my build contains sub folders and files within the sub folders. I can ftp a single file at a time, but it will be difficult to specify all the paths and download individula files. My build structure is: ... (2 Replies)
Discussion started by: gurukottur
2 Replies

10. UNIX for Dummies Questions & Answers

Perl, Pipes, and FTP

I am attempting to automate an ftp session in PERL by emulating the user and sending commands to ftp, but I am getting unexpected and unwanted results. Here is a portion code that illustrates the method I am attempting (this was just a shot in the dark): system("( echo open server sleep 1... (2 Replies)
Discussion started by: murdaugh
2 Replies
Login or Register to Ask a Question