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==--