Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

FTP multiple files

by DV (Initiate)
on Apr 25, 2007 at 09:25 UTC ( [id://611945]=perlquestion: print w/replies, xml ) Need Help??

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

The following script does not copy the *.log files properly. it copies but adds all the files to log file. #!/usr/bin/perl use Net::FTP; #use strict; my @transferFileList = glob "*.log"; foreach my $transferFile(@transferFileList) { $ftp = Net::FTP->new("62.185.44.98", Debug => 0) or die "Cannot connect to 62.185.44.98: $@"; $ftp->login("f1rewall",'car1bbean') or die "Cannot login ", $ftp->message; $ftp->binary(); $remote = "clm"; print $ftp->pwd (); print "\n"; $ftp->cwd($remote) or die "Cannot change working directory ", $ftp->me +ssage; print $ftp->pwd (); print "\n"; $ftp->put($transferFile); $ftp->quit; }

Replies are listed 'Best First'.
Re: FTP multiple files
by Samy_rio (Vicar) on Apr 25, 2007 at 11:00 UTC

    Hi DV, Try like this,

    #!/usr/bin/perl use warnings; use strict; use Net::FTP; use Cwd; $|++; # Autoflush my $server = "172.146.140.140"; my $username = "test"; my $pass = "test"; my $ftp; print "Connecting to $server.."; # Set up connection $ftp = Net::FTP->new( $server, Passive => 1, Debug => 0 ) or die $@; print "..authenticating.."; # Log in... $ftp->login( $username, $pass ) or die $ftp->message; print "..done!\n"; my @transferFileList = glob "*.log"; print $ftp->pwd (), "\n"; $ftp->cwd('somefolder') or die $ftp->message; print $ftp->pwd (), "\n"; $ftp->binary(); for (@transferFileList){ print "$_\n"; $ftp->put(cwd . "\\$_") or die $ftp->message; } print "Logging out.."; $ftp->quit or die $ftp->message; print "..done!\n";

    1. The glob function will listout the files name only(without path).

    2. No need to connect ftp for each and every file.

    3. Always use pragma (strict and warnings).

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: FTP multiple files
by johngg (Canon) on Apr 25, 2007 at 10:33 UTC
    Have you checked the result of the glob? It might not contain the list of files you think it does. Why have you commented out the use strict;? Not a good move, IMHO. Regarding your logic, it might be better to to put the for loop just around the $ftp->put( ... ) so that you are not constantly opening and closing FTP connections.

    I hope your IP, username and password have been anonymised :)

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://611945]
Approved by shigetsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found