problem changing directory in ftp

problem changing directory in ftp

am 05.11.2008 11:57:00 von zilore mumba

--===============0609161037==
Content-Type: multipart/alternative; boundary="0-1235879405-1225882620=:35990"

--0-1235879405-1225882620=:35990
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

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 =3D"/home/Mum=
ba/ARCHIVE/model/date (date is today's date eg 20081105)
In model2 it has to copy from two directories to two local directories =3D"=
/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 an=
d for model2, thus replacing what was earlier copied.
=A0
How do I define more local directories so that they can be changed in the=
=A0"foreach $rdir (@remotedirs)" loop?
=A0
Assistance will be appreciated.
Zilore

$remote_basedir=3D"/home/synergie";
$datadir=3D"/data/grib";
$local_basedir=3D"/home/Mumba/ARCHIVE";
@models=3D("arpege", "ukmo", "ecmf");
$dat=3D`date +%Y%m%d`; chomp $dat;
=A0
# this is a guess so that we don't have to manually edit the script
# and input the cycle each time
$hr=3D`date +%H`; chomp $hr;
if ( $hr > 15 ) { $cycle=3D"12"; }
else { $cycle=3D"00"; }
=A0
# connect to the remote host
#
print "Connecting to $Host ...\n";
$ftp =3D Net::FTP->new($Host, Timeout =3D> 360, Passive=3D>"true") || &fail=
;
$ftp->login($User,$Pass) ;
$ftp->binary ;
$ftp->cwd($datadir);
=A0
# retrieve data
#
print "Copying data\n";
foreach $model (@models) {
   # define local and remote directories before copying files
   #
   $localdir=3D"$local_basedir/$model/$dat";
   undef @remotedirs;
   if ( "$model" eq "arpege" ) {
    =A0 $remotedir=3D"${remote_basedir}${datadir}/PS.R2kAF15/${dat}=
${cycle}0000";
    =A0 push (@remotedirs, $remotedir);
   }=20
   elsif ( "$model" eq "ukmo" ) {
    =A0 $remotedir=3D"${remote_basedir}${datadir}/UK2.G41/${dat}${c=
ycle}0000";
    =A0 push (@remotedirs, $remotedir);
    =A0 $remotedir=3D"${remote_basedir}${datadir}/UK2.G37/${dat}${c=
ycle}0000";
    =A0 push (@remotedirs, $remotedir);
   }
   elsif ( "$model" eq "ecmf") {
    =A0 $remotedir=3D"${remote_basedir}${datadir}/PC.G25/${dat}${cy=
cle}0000";
    =A0 push (@remotedirs, $remotedir);
    =A0 $remotedir=3D"${remote_basedir}${datadir}/PC.G28/${dat}${cy=
cle}0000";
    =A0 push (@remotedirs, $remotedir);
    =A0 $remotedir=3D"${remote_basedir}${datadir}/PC.G29/${dat}${cy=
cle}0000";
    =A0 push (@remotedirs, $remotedir);
    =A0 $remotedir=3D"${remote_basedir}${datadir}/PC.G32/${dat}${cy=
cle}0000";
    =A0 push (@remotedirs, $remotedir);
   }
 
   if (! -d $localdir ) { system("mkdir -p $localdir"); }
   chdir $localdir;
=A0
   # retrieve files
   #
   foreach $rdir (@remotedirs) {=20
    =A0 print "Retrieving files from $rdir to $localdir\n";
=A0
    =A0 $ftp->cwd($rdir);
    =A0 # perl/ftp doesn't support mget, so we have to get a list
    =A0 # and *then* get the files :(
    =A0 #
    =A0 @files =3D $ftp->ls;
    =A0 foreach $file (@files) {=20
      =A0 $ftp->get($file);
    =A0 }
   }
}
$ftp->close;=20
print "copying ends\n";
exit 0;
#===================3D=3 D=====
==================== =====3D=
==================== =====3D=
==
#                  =A0=A 0    =
      =A0 Subroutines
#----------------------------------------------------------- ---------------=
--
sub fail {
  =A0 $ftp->quit;
  =A0 die "ftp error occurred\n";
}

=0A
--0-1235879405-1225882620=:35990
Content-Type: text/html; charset=us-ascii

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?

 

Assistance will be appreciated.

Zilore


$remote_basedir="/home/synergie";
$datadir="/data/grib";
$local_basedir="/home/Mumba/ARCHIVE";

@models=("arpege", "ukmo", "ecmf");

$dat=`date +%Y%m%d`; chomp $dat;

 

# this is a guess so that we don't have to manually edit the script
# and input the cycle each time
$hr=`date +%H`; chomp $hr;
if ( $hr > 15 ) { $cycle="12"; }
else { $cycle="00"; }

 

# connect to the remote host
#
print "Connecting to $Host ...\n";
$ftp = Net::FTP->new($Host, Timeout => 360, Passive=>"true") || &fail ;
$ftp->login($User,$Pass) ;
$ftp->binary ;

$ftp->cwd($datadir);

 

# retrieve data
#
print "Copying data\n";

foreach $model (@models) {

   # define local and remote directories before copying files
   #
   $localdir="$local_basedir/$model/$dat";

   undef @remotedirs;

   if ( "$model" eq "arpege" ) {
      $remotedir="${remote_basedir}${datadir}/PS.R2kAF15/${dat}${c ycle}0000";
      push (@remotedirs, $remotedir);
   }
   elsif ( "$model" eq "ukmo" ) {
      $remotedir="${remote_basedir}${datadir}/UK2.G41/${dat}${cycl e}0000";
      push (@remotedirs, $remotedir);
      $remotedir="${remote_basedir}${datadir}/UK2.G37/${dat}${cycl e}0000";
      push (@remotedirs, $remotedir);
   }
   elsif ( "$model" eq "ecmf") {
      $remotedir="${remote_basedir}${datadir}/PC.G25/${dat}${cycle }0000";
 &
nbsp;    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"); }
   chdir $localdir;
 
   # retrieve files
   #
   foreach $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 :(
      #
      @files = $ftp->ls;
      foreach $file (@files) {
        $ftp->get($file);
      }

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

#=========================================================== =================
#                                Subroutines
#----------------------------------------------------------- -----------------
sub fail {
    $ftp->quit;
    die "ftp error occurred\n";
}







--0-1235879405-1225882620=:35990--

--===============0609161037==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0609161037==--

Re: problem changing directory in ftp

am 05.11.2008 12:35:00 von Bill Luebkert

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