I wrote this code to email me attachments of any files found at a certain
ftp site. It works but... since I'm new at Perl I know there's got to be a
better way. In particular the handling the file transfer by parsing out the
output from DIR seems hinky. Let me know if you can help.
#!c:\perl\perl.exe
#email the files from the FTP site
use strict;
use Net::FTP;
use constant SERVER => 'ftp.xxx.com';
use constant USER => 'user';
use constant PSWD => 'pswd';
use constant FOLDER => './folder';
my ($ftp, @dirlist, @splitlist, $x, @ignore, @attach);
#get DIR from FTP site and add to dirlist---
$ftp = Net::FTP->new(SERVER);
$ftp->login(USER, PSWD);
$ftp->cwd(FOLDER);
foreach ($ftp->dir) {
push (@dirlist, $_)
}
$x = shift (@dirlist); #discard the first element (file count)
push (@ignore, $x);
#get files from dirlist and make an attachments list---
foreach (@dirlist) {
@splitlist = split;
unless ($splitlist[0] =~ /^d/) {
$ftp->get($splitlist[8]); #get file
push (@attach, "-attach $splitlist[8]"); #make
attachments string for blat
} else {
push (@ignore, $splitlist[8]);
}
}
$ftp->quit;
#email files as attachments---
blatme ("@attach");
#print ("@ignore"); #if interested as to what was discarded, should be
directory names
# #and file counts
#subroutines---
sub blatme {
my ($blatcommand);
$blatcommand = "Blat Message.txt -s FTPfiles -t blow.joe\@xxx.com
$_[0]";
system ($blatcommand);
}