jsparrow
MIS
- Jun 7, 2004
- 6
Hello,
Perl newbie needs some help! If a large file, the below FTP-perl script is only uploading part of the file. If it's a small file, the upload is successful.
Our EDI send files all get dumped to one file and then I’m taking that, renaming it, running an external process on them, then pushing to the remote server (FTP). If the file is small, or has one EDI interchange, there’s no issue. The problem arises when there are more than one interchange, and/or the file is large…it then uploads the partial file to the remote server (sometimes even a NULL file). My first thought was maybe perl is running faster then the FTP is responding and it can’t keep up. I tried installing a few pauses (SLEEP commands) but still no luck.
## Define Global Variables
my $Name;
my $xprocess;
my $newname;
my $password;
my $username;
my $remote_dir;
my $host;
my $local_dir;
my $filename;
my $log;
## Assign values to Variables
$host = "host";
$password = "Password";
$username = "username";
$remote_dir = "send";
$local_dir = "C:/Program Files/COMMERCE Connection32/VPN FILES/Send";
$log = "C:/Perl/Programs/ftp-log.txt";
## create system date
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;
if($mon <= 9){$mon = "0$mon"};
if($mday <= 9){$mday = "0$mday"};
## Open log file
open(LOG, ">>$log");
print LOG "***********FTP SEND LOG***********\n";
print LOG "$mon-$mday $hour$min\n";
## create variable for filename
$Name = ("C:/Program Files/COMMERCE Connection32/VPN FILES/Send/send.txt");
## check to make sure the file exists
if (-e "$Name"){
print LOG "$mon-$mday $hour$min file exist $Name\n" ;
}else
{
print LOG "$mon-$mday $hour$min ###ERROR file does NOT exist $Name\n";
}
## create variable for external process - bat file. This bat file will kick off the external process that splits the data
$xprocess = "C:/Perl/Programs/xprocess-send.bat";
## system stop until xprocess is completed (bat file is finished)
system($xprocess);
print LOG "$mon-$mday $hour$min xprocess\n" ;
## create variable adding year, month, day and hour and minute, with the extension *.edi
$newname = "$mon$mday$hour$min.edi";
## rename the send.txt file to date +.edi
rename($Name, "C:/Program Files/COMMERCE Connection32/VPN FILES/Send/$newname") or die print LOG "$mon-$mday $hour$min ###ERROR problem renaming send.txt\n";
print LOG "$mon-$mday $hour$min file renamed\n" ;
## assign variable newname to filename variable - this is the file that will be uploaded to the remote server
$filename = "C:/Program Files/COMMERCE Connection32/VPN FILES/Send/$newname";
print LOG "$mon-$mday $hour$min file named $filename\n";
## check to make sure the file exists
if (-e "$filename"){
print LOG "$mon-$mday $hour$min file does exist $filename \n" ;
}else
{
print LOG "$mon-$mday $hour$min ###ERROR file does not exist $filename\n";
}
## check to make sure the file is readable
if (-r "$filename"){
print LOG "$mon-$mday $hour$min file is readable $filename\n" ;
}else
{
print LOG "$mon-$mday $hour$min ###ERROR: file is not readable $filename\n";
}
open(FILE,$filename);
close(FILE);
use Net::FTP;
## Connect to address
$ftp = Net::FTP->new($host) or die print LOG "$mon-$mday $hour$min ###ERROR Can't connect: $host\n";
print LOG "$mon-$mday $hour$min FTP session started\n";
## Attempt to login
$ftp->login($username, $password) or die print LOG "$mon-$mday $hour$min ###ERROR Can't Login $username, $password\n";
print LOG "$mon-$mday $hour$min Login successful\n";
## Change to remote directory
$ftp->cwd($remote_dir) or die print LOG "$mon-$mday $hour$min ###ERROR Couldn't change directory $remote_dir\n";
print LOG "$mon-$mday $hour$min Changed Directory: $remote_dir\n";
## Transfer file to remote directory
$ftp->put($filename) or die print LOG "$mon-$mday $hour$min ###ERROR Couldn't put $filename\n";
print LOG "$mon-$mday $hour$min Uploaded file\n";
## FTP ends
$ftp->quit;
print LOG "$mon-$mday $hour$min FTP session ends\n";
## move file
use File::Copy;
## move send file to send directory
$status = move($filename, "C:/Program Files/COMMERCE Connection32/VPN FILES/Send/Send History/$newname") or die print LOG "$mon-$mday $hour$min ###ERROR file move failed: $!";
print LOG "$mon-$mday $hour$min file moved to C:/Program Files/COMMERCE Connection32/VPN FILES/Send/Send History\n";
print LOG "$mon-$mday $hour$min ------END FTP SEND------\n";
close(LOG);
Perl newbie needs some help! If a large file, the below FTP-perl script is only uploading part of the file. If it's a small file, the upload is successful.
Our EDI send files all get dumped to one file and then I’m taking that, renaming it, running an external process on them, then pushing to the remote server (FTP). If the file is small, or has one EDI interchange, there’s no issue. The problem arises when there are more than one interchange, and/or the file is large…it then uploads the partial file to the remote server (sometimes even a NULL file). My first thought was maybe perl is running faster then the FTP is responding and it can’t keep up. I tried installing a few pauses (SLEEP commands) but still no luck.
## Define Global Variables
my $Name;
my $xprocess;
my $newname;
my $password;
my $username;
my $remote_dir;
my $host;
my $local_dir;
my $filename;
my $log;
## Assign values to Variables
$host = "host";
$password = "Password";
$username = "username";
$remote_dir = "send";
$local_dir = "C:/Program Files/COMMERCE Connection32/VPN FILES/Send";
$log = "C:/Perl/Programs/ftp-log.txt";
## create system date
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;
if($mon <= 9){$mon = "0$mon"};
if($mday <= 9){$mday = "0$mday"};
## Open log file
open(LOG, ">>$log");
print LOG "***********FTP SEND LOG***********\n";
print LOG "$mon-$mday $hour$min\n";
## create variable for filename
$Name = ("C:/Program Files/COMMERCE Connection32/VPN FILES/Send/send.txt");
## check to make sure the file exists
if (-e "$Name"){
print LOG "$mon-$mday $hour$min file exist $Name\n" ;
}else
{
print LOG "$mon-$mday $hour$min ###ERROR file does NOT exist $Name\n";
}
## create variable for external process - bat file. This bat file will kick off the external process that splits the data
$xprocess = "C:/Perl/Programs/xprocess-send.bat";
## system stop until xprocess is completed (bat file is finished)
system($xprocess);
print LOG "$mon-$mday $hour$min xprocess\n" ;
## create variable adding year, month, day and hour and minute, with the extension *.edi
$newname = "$mon$mday$hour$min.edi";
## rename the send.txt file to date +.edi
rename($Name, "C:/Program Files/COMMERCE Connection32/VPN FILES/Send/$newname") or die print LOG "$mon-$mday $hour$min ###ERROR problem renaming send.txt\n";
print LOG "$mon-$mday $hour$min file renamed\n" ;
## assign variable newname to filename variable - this is the file that will be uploaded to the remote server
$filename = "C:/Program Files/COMMERCE Connection32/VPN FILES/Send/$newname";
print LOG "$mon-$mday $hour$min file named $filename\n";
## check to make sure the file exists
if (-e "$filename"){
print LOG "$mon-$mday $hour$min file does exist $filename \n" ;
}else
{
print LOG "$mon-$mday $hour$min ###ERROR file does not exist $filename\n";
}
## check to make sure the file is readable
if (-r "$filename"){
print LOG "$mon-$mday $hour$min file is readable $filename\n" ;
}else
{
print LOG "$mon-$mday $hour$min ###ERROR: file is not readable $filename\n";
}
open(FILE,$filename);
close(FILE);
use Net::FTP;
## Connect to address
$ftp = Net::FTP->new($host) or die print LOG "$mon-$mday $hour$min ###ERROR Can't connect: $host\n";
print LOG "$mon-$mday $hour$min FTP session started\n";
## Attempt to login
$ftp->login($username, $password) or die print LOG "$mon-$mday $hour$min ###ERROR Can't Login $username, $password\n";
print LOG "$mon-$mday $hour$min Login successful\n";
## Change to remote directory
$ftp->cwd($remote_dir) or die print LOG "$mon-$mday $hour$min ###ERROR Couldn't change directory $remote_dir\n";
print LOG "$mon-$mday $hour$min Changed Directory: $remote_dir\n";
## Transfer file to remote directory
$ftp->put($filename) or die print LOG "$mon-$mday $hour$min ###ERROR Couldn't put $filename\n";
print LOG "$mon-$mday $hour$min Uploaded file\n";
## FTP ends
$ftp->quit;
print LOG "$mon-$mday $hour$min FTP session ends\n";
## move file
use File::Copy;
## move send file to send directory
$status = move($filename, "C:/Program Files/COMMERCE Connection32/VPN FILES/Send/Send History/$newname") or die print LOG "$mon-$mday $hour$min ###ERROR file move failed: $!";
print LOG "$mon-$mday $hour$min file moved to C:/Program Files/COMMERCE Connection32/VPN FILES/Send/Send History\n";
print LOG "$mon-$mday $hour$min ------END FTP SEND------\n";
close(LOG);