Thanks to all of you I am able to create my first serious perl script..
now i want to finetune my program..
As I told you I am ftping a file from one Unix server to another
server..here is my code:
use strict;
use Net::FTP;
my (@allfiles,$h_ftp,
$n,@fArr,$file,$onlyFile,$onlyExt,@confirmationFiles_pgp,$newfile,$tstamp,$d
ot,$hyphen);
#logging into ENTFTP server
$h_ftp = Net::FTP->new('MY.SERVER', Debug => 0);
$h_ftp->login( 'myuserid','mypwd');
#Change to the correct directory
##if ($host1_path ne "nopath")) {
# $h_ftp->cwd($host1_path);
# }
# check if there are files in the directory
@allfiles = $h_ftp->ls();
$n = @allfiles;
if ($n == 0) {
print "No files for download on MY.SERVER";
exit 0;
}
print "total number of files to download $n \n";
# select files which meet the searchstring
foreach $file (@allfiles) {
$onlyFile = ' ';
$onlyExt = ' ';
print "files to be downloaded are $file ";
# split the file into two parts one containing the file name the
other one the extension
@fArr = split(/\./, $file);
$onlyFile = $fArr[0];
if ($fArr[1] ne ' ')
{$onlyExt = $fArr[1];}
print "file name $onlyFile ";
print "file ext $onlyExt \n";
if ($onlyExt eq 'zip')
{push(@confirmationFiles_pgp, $file);}
}
$n = 0;
$n = @confirmationFiles_pgp;
if ($n == 0) {
print "No zip files for download on MY.SERVER ";
exit 0;
}
#download confirmation files
foreach $file (@confirmationFiles_pgp) {
#rename the file
$tstamp = time;
@fArr = split(/\./, $file);
$onlyFile = $fArr[0];
$onlyExt = $fArr[1];
$dot = '.';
$hyphen = '-';
$newfile = $onlyFile.$hyphen.$tstamp.$dot.$onlyExt;
print "newfile name is $newfile";
$h_ftp->get($file,$newfile);
print "$file downloaded from MY.SERVER";
}
$h_ftp->quit;
a)
i am writing all the error message as print command on the STDOUT presently
in my program.
please let me know what is the standard process to create a log of error
mesage. whether i will write into a file all the logs and email it to a
address? if so please let me know the commands to open and write some
message into a file and send it to a email address.
b) please also let me know how to catch any NET::FTP error (e.g: The
connection is not open or the FTP had not taken place properly, the get
command didn't executed etc)
Thanks in advance,
Rajib Sengupta
ConAgra Business Systems
Peoplesoft AR and Order Management
222 South 15th St,North Tower(CT-930)
Omaha NE 68102
Phone: (402) 595-7928,Fax: (402) 595-7024
-----Original Message-----
From: Hemphill, Barry [mailto:[EMAIL PROTECTED]]
Sent: Friday, April 26, 2002 5:45 PM
To: ActivePerl (E-mail)
Cc: '$Bill Luebkert'; Sengupta, Rajib (CC-Contractor)
Subject: RE: Want help for FTP using Perl
-----Original Message-----
From: $Bill Luebkert [mailto:[EMAIL PROTECTED]]
Sent: Friday, April 26, 2002 5:55 PM
Subject: Re: Want help for FTP using Perl
>> Sengupta, Rajib (CC-Contractor) wrote:
>> I had a file called xyz.txt - how to delete the .txt portion and just
have
>> the xyz portion.
> There are too many to mention them all. Easiest would probably be a RE.
>
> $filename =~ s/\..*$//;
Be careful with those regular expressions - sometimes greediness can jump up
and bite you. The solution you're looking for is "take everything up to, but
not including, the last . and any letters after it". The RE above will give
you "take everything up to, but not including, the first .". In a nice
simple case like xyz.txt, the two are the same, but how about for
foo.bar.txt? In that case, $filename would be left with just foo, not
foo.bar. A better solution with RE might be:
$filename =~ s/\.[^\.]*$//;
or
$filename =~ s/(.*)\..*$/$1/;
The first says "match a period, followed by zero or more characters which
are not a period, and replace them with nothing". The second just takes
advantage of greediness. I'm sure there are plenty of other RE solutions as
well.
I also saw that one other poster suggested the following solution:
@fArr = split(/\./, $fileName);
$newFile = $fArr[0];
Once again, this will just take everything up to the first period, rather
than everything but the last. How about this for a solution:
@tmp=split /\./, $filename;
$a=join('.', @tmp[0..$#tmp-1]);
Have fun,
Barry
----
Barry Hemphill - Course Developer / Instructor
Tel: (508) 303-4011 Educational Services
Fax: (508) 486-4555 Concord Communications, Inc.,
[EMAIL PROTECTED] 600 Nickerson Road,
http://www.concord.com Marlboro, MA 01752
"Be very, very careful what you put into that head, because you will never,
ever get it out."
--- Thomas Cardinal Wolsey (1471-1530)
**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
This footnote also confirms that this email message has been swept by
the latest virus scan software available for the presence of computer
viruses.
**********************************************************************
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs