Hey all, I struggled with this code for the last couple days and finally got it working. Its still a bit noisy and could most definitely be refined, but thought I would share it.
Basically, the script is being run on an ftp server, where we are sending 3 files to a read only folder, waiting for a group on the outside to download those files, process them, and upload 9 results files to another write directory, repeat til all files have been processed. The timer is run at 10 seconds. Hope this helps someone else in the future with a similar project.
Cheers,
Eric
[CODE=perl]
#!/usr/local/bin/perl
use strict;
use warnings;
use File::Copy;
my $InDir = "D:\\data\\ecli pse\\workspace\ \contents";
my $file_count_old =-1;
my $pid;
die "cant fork $!\n" unless defined($pid=fo rk());
if($pid) {
print "Here we go\n";
my $in=<STDIN>;
while ($in ne 'stop') {
$in=<STDIN>;
chomp $in;
print "$in is your input\n";
if($in eq 'stop') {
print "exiting....... ..\n";
}
}
}
else {
LOOP1:
sleep 10;
{move_files()}
goto LOOP1;
}
sub move_files {
my $SenDir = "D:\\data\\ecli pse\\workspace\ \contents\\sen" ;
my $SptDir = "D:\\data\\ecli pse\\workspace\ \contents\\spt" ;
my $WeaDir = "D:\\data\\ecli pse\\workspace\ \contents\\wea" ;
my $OutDir = "D:\\data\\ecli pse\\workspace\ \contents\\out" ;
my @dirSen;
opendir(DIR, $SenDir) or die "Unable to open dir $SenDir: $!\n";
@dirSen = sort readdir(DIR);
my $dir_sen_conten ts = $dirSen[2];
close(DIR);
#$dir_sen_conte nts is now the first file of sen directory
my @dirSpt;
opendir(DIR, $SptDir) or die "Unable to open dir $SptDir: $!\n";
@dirSpt = sort readdir(DIR);
my $dir_spt_conten ts = $dirSpt[2];
close(DIR);
#$dir_spt_conte nts is now the first file of spt directory
my @dirWea;
opendir(DIR, $WeaDir) or die "Unable to open dir $WeaDir: $!\n";
@dirWea = sort readdir(DIR);
my $dir_wea_conten ts = $dirWea[2];
close(DIR);
#$dir_wea_conte nts is now the first file of wea directory
my $directory_coun t=0;
my $file_count=0;
opendir(DIR, $InDir);
LINE: while(my $FILE = readdir(DIR)) {
next LINE if($FILE =~ /^\.\.?/);
## check to see if it is a directory
if(-d "$FILE"){
$directory_coun t++;
}
else {
$file_count++;
}
}
closedir(DIR);
my $divisor = 9;
my $remainder = $file_count % $divisor;
my $FilesLeft = ($divisor - $remainder);
my $tmp_sen_in ="$SptDir\\$dir _spt_contents";
my $tmp_spt_in="$S enDir\\$dir_sen _contents";
my $tmp_wea_in="$W eaDir\\$dir_wea _contents";
my $tmp_sen_out="$ OutDir\\$dir_sp t_contents";
my $tmp_spt_out="$ OutDir\\$dir_se n_contents";
my $tmp_wea_out="$ OutDir\\$dir_we a_contents";
my $file_out="temp ";
my $nav_date=subst r $dir_spt_conten ts,1,6;
(my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localti me(time);
open (FILE, ">>$file_ou t") || die "Cannot write to $file_out\n";
if (($remainder==0 ) && ($file_count_ol d<$file_count)) {
$file_count_old =$file_count;
print "$file_count_ol d\n";
move ($tmp_sen_in, $tmp_sen_out) or die "failed to move";
move ($tmp_spt_in, $tmp_spt_out) or die "failed to move";
move ($tmp_wea_in, $tmp_wea_out) or die "failed to move";
print "it worked! we moved it at :",$year+190 0,"-",$mon+1,"-",$mday," ",$hour,":",$mi n,":",$sec,"\n\ n";
while(<FILE>){
print "$dir_spt_conte nts$dir_sen_con tents$dir_wea_c ontents.txt";
}
close (FILE);
rename "$file_out","$O utDir\\$file_ou t\_$nav_date" || die "Cannot update $file_out\n";
}
else {
print "They are currently uploading: $FilesLeft files left don't move em at ",$year+190 0,"-",$mon+1,"-",$mday," ",$hour,":",$mi n,":",$sec,"\ n"
}
}
kill ("TERM",$pid ); [/CODE]
Basically, the script is being run on an ftp server, where we are sending 3 files to a read only folder, waiting for a group on the outside to download those files, process them, and upload 9 results files to another write directory, repeat til all files have been processed. The timer is run at 10 seconds. Hope this helps someone else in the future with a similar project.
Cheers,
Eric
[CODE=perl]
#!/usr/local/bin/perl
use strict;
use warnings;
use File::Copy;
my $InDir = "D:\\data\\ecli pse\\workspace\ \contents";
my $file_count_old =-1;
my $pid;
die "cant fork $!\n" unless defined($pid=fo rk());
if($pid) {
print "Here we go\n";
my $in=<STDIN>;
while ($in ne 'stop') {
$in=<STDIN>;
chomp $in;
print "$in is your input\n";
if($in eq 'stop') {
print "exiting....... ..\n";
}
}
}
else {
LOOP1:
sleep 10;
{move_files()}
goto LOOP1;
}
sub move_files {
my $SenDir = "D:\\data\\ecli pse\\workspace\ \contents\\sen" ;
my $SptDir = "D:\\data\\ecli pse\\workspace\ \contents\\spt" ;
my $WeaDir = "D:\\data\\ecli pse\\workspace\ \contents\\wea" ;
my $OutDir = "D:\\data\\ecli pse\\workspace\ \contents\\out" ;
my @dirSen;
opendir(DIR, $SenDir) or die "Unable to open dir $SenDir: $!\n";
@dirSen = sort readdir(DIR);
my $dir_sen_conten ts = $dirSen[2];
close(DIR);
#$dir_sen_conte nts is now the first file of sen directory
my @dirSpt;
opendir(DIR, $SptDir) or die "Unable to open dir $SptDir: $!\n";
@dirSpt = sort readdir(DIR);
my $dir_spt_conten ts = $dirSpt[2];
close(DIR);
#$dir_spt_conte nts is now the first file of spt directory
my @dirWea;
opendir(DIR, $WeaDir) or die "Unable to open dir $WeaDir: $!\n";
@dirWea = sort readdir(DIR);
my $dir_wea_conten ts = $dirWea[2];
close(DIR);
#$dir_wea_conte nts is now the first file of wea directory
my $directory_coun t=0;
my $file_count=0;
opendir(DIR, $InDir);
LINE: while(my $FILE = readdir(DIR)) {
next LINE if($FILE =~ /^\.\.?/);
## check to see if it is a directory
if(-d "$FILE"){
$directory_coun t++;
}
else {
$file_count++;
}
}
closedir(DIR);
my $divisor = 9;
my $remainder = $file_count % $divisor;
my $FilesLeft = ($divisor - $remainder);
my $tmp_sen_in ="$SptDir\\$dir _spt_contents";
my $tmp_spt_in="$S enDir\\$dir_sen _contents";
my $tmp_wea_in="$W eaDir\\$dir_wea _contents";
my $tmp_sen_out="$ OutDir\\$dir_sp t_contents";
my $tmp_spt_out="$ OutDir\\$dir_se n_contents";
my $tmp_wea_out="$ OutDir\\$dir_we a_contents";
my $file_out="temp ";
my $nav_date=subst r $dir_spt_conten ts,1,6;
(my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localti me(time);
open (FILE, ">>$file_ou t") || die "Cannot write to $file_out\n";
if (($remainder==0 ) && ($file_count_ol d<$file_count)) {
$file_count_old =$file_count;
print "$file_count_ol d\n";
move ($tmp_sen_in, $tmp_sen_out) or die "failed to move";
move ($tmp_spt_in, $tmp_spt_out) or die "failed to move";
move ($tmp_wea_in, $tmp_wea_out) or die "failed to move";
print "it worked! we moved it at :",$year+190 0,"-",$mon+1,"-",$mday," ",$hour,":",$mi n,":",$sec,"\n\ n";
while(<FILE>){
print "$dir_spt_conte nts$dir_sen_con tents$dir_wea_c ontents.txt";
}
close (FILE);
rename "$file_out","$O utDir\\$file_ou t\_$nav_date" || die "Cannot update $file_out\n";
}
else {
print "They are currently uploading: $FilesLeft files left don't move em at ",$year+190 0,"-",$mon+1,"-",$mday," ",$hour,":",$mi n,":",$sec,"\ n"
}
}
kill ("TERM",$pid ); [/CODE]
Comment