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!

Problem adding IF statement while doing FTP process 2

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I have code that works, only it sends an end of process file ($vName2) even if no files were sent.

Code:
if (-e $vName1)
{
    $rc=system("del /Q $vName1");
    chdir ($dir);
  
    opendir($dh, $dir) or die "can't opendir $dir: $!";
    @files = readdir($dh);

    $ftp=Net::FTP->new($host, Debug => 0) or die "No connection";
    $ftp->login($username, $password) or die "cannot login", $ftp->message;
    $ftp->cwd (Rightfax) or die "can't cwd ", $ftp->message;
    $ftp->binary();
   
    for $file (@files)
    {
        chomp @files;
        next if $file =~ /^\.{1,2}$/;   # skip file if . or ..
        $ftp->put($file) or die "put failed ", $ftp->message;
    }
    $ftp->quit;
    closedir $dh;

    $rc=system("del /Q *.pdf");

    chdir ($dir2);
  
    opendir($dh, $dir2) or die "can't opendir $dir: $!";
    @files = readdir($dh);

    $ftp=Net::FTP->new($host, Debug => 0) or die "No connection";
    $ftp->login($username, $password) or die "cannot login", $ftp->message;
    $ftp->cwd (Rightfax) or die "can't cwd ", $ftp->message;
    $ftp->binary();
   
    for $file (@files)
    {
        chomp @files;
        next if $file =~ /^\.{1,2}$/;   # skip file if . or ..
        $ftp->put($file) or die "put failed ", $ftp->message;
	  $filefound = 1;
    }

    $ftp->put($vName2) or die "last put failed", $ftp->message;

    $ftp->quit;
    closedir $dh;

    $rc=system("del /Q *.txt");
}

I tried adding a if statement to see if $filefound was equal to 1. That caused the code to fail. I then tried checking if $filefound was greater than 0. That also failed.

Can somebody tell me what I am doing wrong?
 
Hi carrot,

Can you show me the code with the $filefound comparision?

I suspect it's just variable scope. Unless you have defined all the variables prior to the code you have posted with 'my's or 'our's, the $filefound will only exist in the scope of the for ()

Try setting $filefound = 0 before the for first and see if that lets you check the value.

Cheers,
Scott
 
Here is the code that I used.

Code:
$filefound = 0;

if (-e $vName1)
{
    $rc=system("del /Q $vName1");
    chdir ($dir);
  
    opendir($dh, $dir) or die "can't opendir $dir: $!";
    @files = readdir($dh);

    $ftp=Net::FTP->new($host, Debug => 0) or die "No connection";
    $ftp->login($username, $password) or die "cannot login", $ftp->message;
    $ftp->cwd (Rightfax) or die "can't cwd ", $ftp->message;
    $ftp->binary();
   
    for $file (@files)
    {
        chomp @files;
        next if $file =~ /^\.{1,2}$/;   # skip file if . or ..
        $ftp->put($file) or die "put failed ", $ftp->message;
    }
    $ftp->quit;
    closedir $dh;

    $rc=system("del /Q *.pdf");

    chdir ($dir2);
  
    opendir($dh, $dir2) or die "can't opendir $dir: $!";
    @files = readdir($dh);

    $ftp=Net::FTP->new($host, Debug => 0) or die "No connection";
    $ftp->login($username, $password) or die "cannot login", $ftp->message;
    $ftp->cwd (Rightfax) or die "can't cwd ", $ftp->message;
    $ftp->binary();
   
    for $file (@files)
    {
        chomp @files;
        next if $file =~ /^\.{1,2}$/;   # skip file if . or ..
        $ftp->put($file) or die "put failed ", $ftp->message;
	  $filefound = 1;
    }

    if ($filefound == 1) {
        $ftp->put($vName2) or die "last put failed", $ftp->message;
    }

    $ftp->quit;
    closedir $dh;

    $rc=system("del /Q *.txt");
}
 
ok - I gather that did not work. I tried it myself and it seems the scope is okay...

I spotted one problem, but it may not be causing your problem - you are chomp'ing @files, when you should be chomp'ing $file. That will most probably cause the next regex to not match on the directory listings as they will still have the \n at the end.

If that does not work, can you get me a dir listing so I can try it out myself? A dump of @files will be fine.

Cheers,
Scott
 
[tt]chomp@files[/tt] is not only incorrect, it's also useless, as [tt]readdir[/tt] doesn't append \n to file names.
Note also that you can filter file names with
[tt]@files=grep{!/^\.{1,2}$/}readdir($dh);[/tt]
This way [tt]scalar@files[/tt] will be zero if there are no matching files and you can do the test with [tt]if(@files){[/tt] instead of using [tt]$filefound[/tt]
Anyway all this probably doesn't solve your problem. You should give us more info though, particularly the error message(s) and whether the code fails with no files, or when one or more files are present, or both.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top