Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wildcard or caputuring filename dynamically

Status
Not open for further replies.

rozzay

Programmer
Jan 3, 2002
142
US
Hi! I have a perl script that renames and ftp files to another server dynamically(whenever a file comes in the script would automatically kick off).
However from time to time ftp connection would get hosed up so if ftp connection fails I have the perl script ot place the failed file in a tmp folder and would have to manually edit the perl script by specifically naming the file
($instream_filename = '/usr/local/cyclone/data/ZZAAFES/old/tmp/a605185ZZAAFESin';)
I needed to be rename/ftp to the specific server. This can get a bit tedius for if 15 files failed then i would have to edit the perl script to the specific filename and run the perl script and repeat.
I would like to automate this process. But I am having the problems of wildcard or capturing the file. Below is the script i am currently using for the manual process. Thanks in advance
==========================================================
#!/usr/bin/perl
use File::Copy;
use File::Basename;

my $now=localtime;
open(LOG, ">>/usr/local/cyclone/logs/GXS_IMFP.log");
open(REPORT, ">>/usr/local/cyclone/logs/IMFP_RENAME.log");

print LOG "$now->GXS_IMFP.pl script STARTED!\n";

#FILE NAMES DEFINED
$temp_path = '/usr/local/cyclone/data/ZZAAFES/old/tmp';
$archive_path= '/usr/local/cyclone/data/ZZAAFES/old/';

#$instream_filename = $ARGV[0];
$instream_filename = '/usr/local/cyclone/data/ZZAAFES/old/tmp/a605185ZZAAFESin';
$cyclone_filename = $instream_filename;

#UNCOMMENT THE ONE BELOW
$instream_short_filename = basename($instream_filename);
$temp_filename = $temp_path.$instream_short_filename;
$archive_filename = $archive_path.$instream_short_filename;

my ($sec,$min,$hours,$mday,$month,$year,$wday,$yday,$isdst)=localtime();
$year+=1900;
$month+=1;

if ($month < 10) {
$month="0".$month;
}
if ($mday < 10) {
$mday="0".$mday;
}
if($hours < 10) {
$hours="0".$hours;
}
if($min < 10) {
$min="0".$min;
}
if($sec < 10) {
$sec="0".$sec;
}

$endpoint = length ($instream_short_filename);
$short_filename = substr($instream_short_filename, 0, $endpoint -9);
$newfilename= "gxs.tmp";
#$retekfname="gxs_$year$month$mday.$hours$min$sec.1";
$retekfname="gxs_" . $year . $month . $mday . "_" . $hours . $min . $sec . "_" . $short_filename . ".1";

#FTP ROUTINE CALLED
$ftpsub_rc=0;
$ftpsub_rc=ftpsub($cyclone_filename);
if ($ftpsub_rc == 0) {
print LOG "FILE FOUND\n";
}

#CHECK TO SEE IF INITIAL FTP FAILED, WAITS 5 MINS AND ATTEMPTS TO RESTART FTP
if ($ftpsub_rc ==1) {
print LOG "FTP failed, attempting to send again\n";
sleep(300);
$ftpsub_rc=ftpsub($cyclone_filename);
}

#CHECK TO SEE IF FTP RESTART FAILED AND SEND EMAIL NOTIFICATION
if ($ftpsub_rc == 1) {
print LOG "FTP failed 2nd attempt, sending email notification\n";
copy $cyclone_filename, $temp_filename;
$emailsub_rc=emailsub($cyclone_filename);
print LOG "Email subroutine return code is: $emailsub_rc";
}

move $cyclone_filename, $archive_filename;
print LOG "$now->GXS_IMFP.pl script completed\n";

#INITIATES FTP CONNECTION TO server.com AND RENAMES FILE
sub ftpsub {
print LOG "$now->FTP CONNECT TO server.com STARTED\n";
use Net::FTP;
my $ftp;
$ftp = Net::FTP->new("server.com", port =>21) || ((print LOG "$now->ERROR:FTP CONNECT TO server.com FAILED\n"), return(1));
$ftp->login("logid", "pwd") || ((print LOG "$now->ERROR:FTP LOGIN TO server.com FAILED\n"), return(1));
$ftp->put($instream_filename, $newfilename) || ((print LOG "$now->ERROR:FTP PUT TO server.com FAILED\n"), return(1));
$ftp->rename($newfilename, $retekfname) || ((print LOG "$now->ERROR:FTP RENAME TO server.com FAILED\n"),return(1));
$ftp->quit();
print LOG "$now->FTP TO server.com COMPLETED\n";
return (0);
};
print REPORT "$now->$instream_short_filename RENAME $retekfname\n";
 
Why not just have the FTP sub. loop through the outgoing directory? Don't bother moving the file to another directory, rather use a flag.

So instream file gets created set $flag = 1 (meaning a file is in the outgoing directory) while $flag= 1 ftp, after successful ftp sent a return code from the ftp sub and $flag = 0 (no file in outgoing dir) else call the ftp sub again until it goes....

Throw a counter in there too... everytime the ftp sub is called, $counter++, then if $counter==10, move file to the bad directory and check it out cuz there's a problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top