Another bug, the "if (! -d $ddir)" command in your highlighted foreach
loop checks your local hard drive to see if the directory exists, not
the remote server you are connecting to, which is what I assume you
meant.
 
I don't see a check if directory or file exists command, so you'd have
to cwd() to that dir & verify it succeeded, or parse what's returned by
list() or nlst().  If it failed, then you can assume the directory
doesn't exist.
 
 As for your 2nd highlighted block, you said "ftp->put" instead of
"$ftp->put".  Makes a world of difference.
 
________________________________

From: [email protected]
[mailto:[email protected]] On Behalf Of Jeff
Saxton
Sent: Tuesday, December 29, 2009 6:45 AM
To: zilore mumba; [email protected]
Subject: RE: help with ftp


from the pod:
 
mkdir ( DIR [, RECURSE ]) 
Create a new directory with the name DIR. If RECURSE is true then mkdir
will attempt to create all the directories in the given path.

 

 

________________________________

From: [email protected]
[[email protected]] On Behalf Of zilore mumba
[[email protected]]
Sent: Tuesday, December 29, 2009 6:00 AM
To: [email protected]
Subject: help with ftp


Hello Perl Community.
Thanks so much for all the help provided lduring the current year.
Happy and prosperous New Year 2010 to all.

Below is my code where I have a problem with ftp. I am trying to
transfer data from one PC to another. When I try to make a directory on
the distant machine, I get the error: "Can't locate object method
'mkpath' via package 'ftp".
When I comment this part out, the same error appears later on with
"ftp->put".
Both parts with problem are highlighted in yellow.

I am not able to find help in Perldoc.
Any help will be appreciated

Zilore


#!/usr/bin/perl -w
#This transfers files from a  PC running LINUX to Windows PC.
#
use Net::FTP;

use strict;
use warnings;
use POSIX;
use File::Path;
use File::Copy;

my $debug = 1;
# set variables
#
my $User = 'pc-mis';
my $Pass = 'acmad11';
my $Host = '192.168.2.115';

my @models = ('arpege', 'ukmo', 'ecmf');
my @now = localtime;
my $dat = strftime "%Y%m%d", @now;
my $mon_ab = strftime "%b",@now;
my $mon_num = strftime "%m",@now;
my $mon_name = strftime "%B",@now;
my $yr=strftime "%y",@now;

my $dash="-";
my $mod = "models";

my ($day, $month, $year) = (localtime(time() - 60 * 60 * 24)) [ 3, 4, 5
];
$year = $year+1900;
$month = $month+1;

my $dat0 = sprintf("%04d%02d%02d",  $year, $month, $day );

my $archive_dir = "../ARCHIVE/$mon_num$dash$mon_name$yr";   # where data
is

my $remote_basedir = '/';
my $data_dir = "ARCHIVE/$mon_num$dash$mon_name$yr/$mon_ab$yr$dash$mod";
#where to put it

my @localdir =("../ARCHIVE/$mon_num$dash$mon_name$yr/$models[0]/$dat0",
               "../ARCHIVE/$mon_num$dash$mon_name$yr/$models[1]/$dat0",
               "../ARCHIVE/$mon_num$dash$mon_name$yr/$models[2]/$dat0");


#my @remotedir =
('$data_dir/$models[0]/$dat0','$data_dir/$models[1]/$dat0',
#                 '$data_dir/$models[2]/$dat0');

# connect to the remote host

print "Connecting to $Host ...\n";
my $ftp = Net::FTP->new($Host, Timeout => 360, Passive => 'true') or
fail ();
    
$ftp->login($User,$Pass) or die " login '$ftp'  failed: $_ ($^E)";
$ftp->binary;

foreach my $ddir (@remotedir) {
    if (! -d $ddir) {
        print "mkpath $ddir\n" if $debug;
        $ftp->mkpath ($ddir) or die "mkpath '$ddir' failed: $! ($^E)";
   }
}
$ftp->cwd($data_dir);

# transfer data

print "Transfering data\n";

#my %numbers = ('$localdirs[0]' => '0','localdirs[1]' =>
'1','localdirs[2]' => '2');

my %remotedirs = ();
my $remotedirs;
    
foreach my $model (@models) {

    # define local and remote directories before copying files

    if ($model eq 'arpege') {
     my $remotedirs = "$data_dir/$models[0]/$dat0";
       $remotedirs{$remotedirs} = $localdir[0];

  } elsif ($model eq 'ukmo') {
     my $remotedirs = "$data_dir/$models[1]/$dat0";
       $remotedirs{$remotedirs} = $localdir[1];

  } elsif ($model eq 'ecmf') {
     my $remotedirs = "$data_dir/$models[2]/$dat0";
       $remotedirs{$remotedirs} = $localdir[2];
  }
  
    # Transfer files

    foreach my $rdir (keys %remotedirs) {
     $ftp->cwd($remotedirs($rdir));

     my $ldir = $remotedirs{$rdir}
     
     print "Transfering files from $ldir to $remotedirs(rdir)\n" if
$debug;
     opendir DIR, "$ldir" or die "opendir '$ldir: $! ($^E)";
     while (my $file = readdir DIR) {
        print "DIR";
        exit;
        next if $file =~ /\*(b|x|l)$/;
        print "Putting $file\n" if $debug;
        ftp->put($file) or warn "Failed '$file': $! ($^E)";
     }
     closedir DIR;
  
    }
}

$ftp->close;
print "Transfering ends\n";
exit 0;

#=======================================================================
=====
#                                Subroutines
#=======================================================================
=====

sub fail {

$ftp->quit;
die "ftp error occurred\n";

}


__END__

my $files = $ftp->dir() or die "Error doing ftp->dir: $!\n";
my $files = dir() or die "Error doing dir: $!\n";
foreach (@$files) {
    # skip lines not starting with d or -
    if (/^[^d-]/) {
         print "Skipping garbage dir line\n" if $debug;
        next;
    }

# ftp dir listing
#----0----- -1- ---2---- ---3---- ---4---- -5- -6 --7-- -----8--------
#total 52
#drwx--x---   3 owner    group        1024 Dec 13  1997 .
#drwxr-xr-x 671 owner    group       49152 Dec 18 14:09 ..
#-rw-r--r--   1 xyz      httpd        2010 Sep 21 13:31 index.htm

    my ($size, $file) = (split /\s+/, $_, 9)[4,8];    # get
size/filename
    next if $file =~ /^\.{1,2}$/;        # skip . and ..

    # if directory - call directory recurse routine

    if (/^d/) {
        ...


        

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to