zilore mumba wrote:
> Assistance sought. Below i have a programme which copies files by ftp. 
> The programme works. Here's what it does
> .1 in the foreach $model (@models) loop it goes thru" one of three 
> models. In model1 it copies from one directory to the local directory 
> ="/home/Mumba/ARCHIVE/model/date (date is today's date eg 20081105)
> In model2 it has to copy from two directories to two local directories 
> ="/home/Mumba/ARCHIVE/model/date and "/home/Mumba/ARCHIVE/model/date+1
> In model3 it has to copy from 4 remote directories.
> Currently it is putting all files in the same local directory for model1 
> and for model2, thus replacing what was earlier copied.
>  
> How do I define more local directories so that they can be changed in 
> the "foreach $rdir (@remotedirs)" loop?

I'm not sure I understand 100% your question, but see if the below
handles it - if not, expand your question a bit.

I recoded your code my way (see if you like it or not), but always use
the first two lines when submitting scripts for group perusal.  I took
your code that shelled out to the system for date/mkdir out and used
modules instead.

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

my $User = 'User';
my $Pass = 'Pass';
my $Host = 'Host';

my $remote_basedir = '/home/synergie';
my $datadir = '/data/grib';
my $local_basedir = '/home/Mumba/ARCHIVE';
my @models = ('arpege', 'ukmo', 'ecmf');
my @now = localtime;
# my $dat = `date +%Y%m%d`; chomp $dat; # don't shell out if you don't have too
my $dat = strftime "%Y%m%d", @now;

# this is a guess so that we don't have to manually edit the script
# and input the cycle each time

# my $hr = `date +%H`; chomp $hr;       # don't shell out if you don't have too
my $hr = strftime "%H", @now;
my $cycle = '00'; $cycle = '12' if $hr > 15;

# connect to the remote host

print "Connecting to $Host ...\n";      # $Host not defined
my $ftp = Net::FTP->new($Host, Timeout => 360, Passive => 'true') or fail ();
$ftp->login($User,$Pass);
$ftp->binary;
$ftp->cwd($datadir);

# retrieve data

print "Copying data\n";
foreach my $model (@models) {

        # define local and remote directories before copying files

        my @remotedirs = ();

        my $localdir = "$local_basedir/$model/$dat";
        my $remotedir;

        if ($model eq 'arpege') {
                $remotedir =
                  "${remote_basedir}${datadir}/PS.R2kAF15/${dat}${cycle}0000";
                push (@remotedirs, $remotedir);
        } elsif ($model eq 'ukmo') {
                $remotedir =
                  "${remote_basedir}${datadir}/UK2.G41/${dat}${cycle}0000";
                push (@remotedirs, $remotedir);
                $remotedir =
                  "${remote_basedir}${datadir}/UK2.G37/${dat}${cycle}0000";
                push (@remotedirs, $remotedir);
        } elsif ($model eq 'ecmf') {
                $remotedir =
                  "${remote_basedir}${datadir}/PC.G25/${dat}${cycle}0000";
                push (@remotedirs, $remotedir);
                $remotedir =
                  "${remote_basedir}${datadir}/PC.G28/${dat}${cycle}0000";
                push (@remotedirs, $remotedir);
                $remotedir =
                  "${remote_basedir}${datadir}/PC.G29/${dat}${cycle}0000";
                push (@remotedirs, $remotedir);
                $remotedir =
                  "${remote_basedir}${datadir}/PC.G32/${dat}${cycle}0000";
                push (@remotedirs, $remotedir);
        }

        if (! -d $localdir) {
#               system ("mkdir -p $localdir");  # don't shell out
                mkpath ($localdir);
        }
        chdir $localdir;

        # retrieve files

        foreach my $rdir (@remotedirs) {

                print "Retrieving files from $rdir to $localdir\n";
                $ftp->cwd($rdir);

# perl/ftp doesn't support mget, so we have to get a list
# and *then* get the files :(

                my @files = $ftp->ls;
                foreach my $file (@files) {
                        # you might want to check if $file is a dir here
                        # see code after __END__ for example
                        $ftp->get($file);
                }
        }
}
$ftp->close;
print "copying 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";
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to