Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FTP inside a loop

Status
Not open for further replies.

emiacs

IS-IT--Management
Apr 7, 2006
6
US
Hi Guys,

I am new to perl scripting. I'm trying to get files from some servers. The names of the servers are stored in a file.

Now if I use this code, it doesn't work:

open(INFO, "$txtfile"); # Open the file
while (<INFO>) {
($var1, $var2) = split(/ /,$_,2);
$line = ftpvar("$var1", "$var2");
print("$line\n");
}
close(INFO);

sub ftpvar{
$myvar1 = $_[0];
$myvar2 = $_[1];

print("$myvar1\n");
print("$myvar2\n");

my $ftp = Net::FTP->new("$myvar2", Debug=>1, Timeout => 120);
return (error=>'Could not login to FTP server') unless $ftp;

$ftp->login("user",'password');
$ftp->cwd("$source_dir");
@dirList = $ftp->ls("*$myvar1*");
$ftp->binary();
foreach $file (@dirList) {
$ftp->get("$file", "$copy_dir/$file");
}
$ftp->quit();
}

But if do it like this, it works:

$var1 = "value1";
$var2 = "value2";
$line = ftpvar("$var1", "$var2");

sub ftpvar{
$myvar1 = $_[0];
$myvar2 = $_[1];

print("$myvar1\n");
print("$myvar2\n");

my $ftp = Net::FTP->new("$myvar2", Debug=>1, Timeout => 120);
return (error=>'Could not login to FTP server') unless $ftp;

$ftp->login("user",'password');
$ftp->cwd("$source_dir");
@dirList = $ftp->ls("*$myvar1*");
$ftp->binary();
foreach $file (@dirList) {
$ftp->get("$file", "$copy_dir/$file");
}
$ftp->quit();
}

I have verified that the values I pass in ftpvar is correct. I really am lost now what is wrong with the code. Thanks for the help!
 
I think you're missing a chonp after reading from the INFO filehandle
Code:
open(INFO, "$txtfile");              # Open the file
while (<INFO>) {
        [b][COLOR=red]chomp $_;[/color][/b]
        ($var1, $var2) = split(/ /,$_,2);
        $line = ftpvar("$var1", "$var2");
        print("$line\n");
}
close(INFO);

sub ftpvar{
    $myvar1 = $_[0];
    $myvar2 = $_[1];

    print("$myvar1\n");
    print("$myvar2\n");

my $ftp =  Net::FTP->new("$myvar2", Debug=>1, Timeout => 120);
return (error=>'Could not login to FTP server') unless $ftp;

    $ftp->login("user",'password');
    $ftp->cwd("$source_dir");
    @dirList = $ftp->ls("*$myvar1*");
    $ftp->binary();
    foreach $file (@dirList) {
        $ftp->get("$file", "$copy_dir/$file");
    }
    $ftp->quit();
}
HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thanks Paul, already tried inserting chomp and still doesn't work... error is still 'Could not login to FTP server'...

There is no error if I don't use it within the loop.
 
Move your connection outside the loop
Code:
open(INFO, "$txtfile");              # Open the file
my $ftp =  Net::FTP->new("$myvar2", Debug=>1, Timeout => 120);
return (error=>'Could not login to FTP server') unless $ftp;

$ftp->login("user",'password');
while (<INFO>) {
        chomp $_;
        ($var1, $var2) = split(/ /,$_,2);
        $line = ftpvar("$var1", "$var2");
        print("$line\n");
}
close(INFO);
$ftp->quit();

sub ftpvar{
    $myvar1 = $_[0];
    $myvar2 = $_[1];
    print("$myvar1\n");
    print("$myvar2\n");
    $ftp->cwd("$source_dir");
    @dirList = $ftp->ls("*$myvar1*");
    $ftp->binary();
    foreach $file (@dirList) {
        $ftp->get("$file", "$copy_dir/$file");
    }
}
HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I have to place it inside the loop because the ftp server is passed as a parameter to the function. I'm logging on to different ftp servers.

Anyway, I have found a work-around. I used the working code and used arguments which I call from another perl program.

Thanks for the inputs.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top