By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,276 Members | 1,532 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,276 IT Pros & Developers. It's quick & easy.

Timed Proceedure

P: 58
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
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Copy;
  5. my $InDir = "D:\\data\\eclipse\\workspace\\contents";
  6. my $file_count_old=-1;
  7. my $pid;
  8. die "cant fork $!\n" unless defined($pid=fork());
  9.  
  10.  if($pid) {
  11.  print "Here we go\n";
  12.  my $in=<STDIN>;
  13.    while ($in ne 'stop') {
  14.    $in=<STDIN>;
  15.    chomp $in;
  16.    print "$in is your input\n";
  17.        if($in eq 'stop') {
  18.        print "exiting.........\n";
  19.        }
  20.    }
  21.  }
  22.  else {
  23. LOOP1:
  24.   sleep 10;
  25.   {move_files()}
  26.  
  27. goto LOOP1;
  28.  
  29.  }
  30.  
  31. sub move_files {
  32.   my $SenDir = "D:\\data\\eclipse\\workspace\\contents\\sen";
  33.   my $SptDir = "D:\\data\\eclipse\\workspace\\contents\\spt";
  34.   my $WeaDir = "D:\\data\\eclipse\\workspace\\contents\\wea";
  35.   my $OutDir = "D:\\data\\eclipse\\workspace\\contents\\out";
  36.  
  37.   my @dirSen;  
  38.     opendir(DIR, $SenDir) or die "Unable to open dir $SenDir: $!\n";
  39.     @dirSen = sort readdir(DIR);
  40.     my $dir_sen_contents = $dirSen[2];
  41.     close(DIR);
  42.     #$dir_sen_contents is now the first file of sen directory
  43.  
  44.   my @dirSpt;  
  45.     opendir(DIR, $SptDir) or die "Unable to open dir $SptDir: $!\n";
  46.     @dirSpt = sort readdir(DIR);
  47.     my $dir_spt_contents = $dirSpt[2];
  48.     close(DIR);
  49.     #$dir_spt_contents is now the first file of spt directory
  50.  
  51.   my @dirWea;  
  52.     opendir(DIR, $WeaDir) or die "Unable to open dir $WeaDir: $!\n";
  53.     @dirWea = sort readdir(DIR);
  54.     my $dir_wea_contents = $dirWea[2];
  55.     close(DIR);
  56.     #$dir_wea_contents is now the first file of wea directory
  57.  
  58.   my $directory_count=0;
  59.   my $file_count=0;
  60.  
  61. opendir(DIR, $InDir);
  62.   LINE: while(my $FILE = readdir(DIR)) {
  63.     next LINE if($FILE =~ /^\.\.?/);
  64.     ## check to see if it is a directory
  65.     if(-d "$FILE"){
  66.       $directory_count++;
  67.     }
  68.     else {
  69.       $file_count++;
  70.     }
  71.   }
  72. closedir(DIR);
  73.  
  74. my $divisor = 9;
  75. my $remainder = $file_count % $divisor;
  76. my $FilesLeft = ($divisor - $remainder);
  77. my $tmp_sen_in ="$SptDir\\$dir_spt_contents";
  78. my $tmp_spt_in="$SenDir\\$dir_sen_contents";
  79. my $tmp_wea_in="$WeaDir\\$dir_wea_contents";
  80. my $tmp_sen_out="$OutDir\\$dir_spt_contents";
  81. my $tmp_spt_out="$OutDir\\$dir_sen_contents";
  82. my $tmp_wea_out="$OutDir\\$dir_wea_contents";
  83. my $file_out="temp";
  84. my $nav_date=substr $dir_spt_contents,1,6;
  85. (my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localtime(time);
  86. open (FILE, ">>$file_out") || die "Cannot write to $file_out\n";
  87.  
  88. if (($remainder==0) && ($file_count_old<$file_count)){
  89.   $file_count_old=$file_count;
  90.   print "$file_count_old\n";
  91.   move ($tmp_sen_in, $tmp_sen_out) or die "failed to move";
  92.   move ($tmp_spt_in, $tmp_spt_out) or die "failed to move";
  93.   move ($tmp_wea_in, $tmp_wea_out) or die "failed to move";
  94.   print "it worked! we moved it at :",$year+1900,"-",$mon+1,"-",$mday," ",$hour,":",$min,":",$sec,"\n\n";
  95.   while(<FILE>){
  96.     print "$dir_spt_contents$dir_sen_contents$dir_wea_contents.txt";
  97.   }
  98.   close (FILE);
  99.   rename "$file_out","$OutDir\\$file_out\_$nav_date" || die "Cannot update $file_out\n";
  100. }
  101. else {
  102.   print "They are currently uploading: $FilesLeft files left don't move em at ",$year+1900,"-",$mon+1,"-",$mday," ",$hour,":",$min,":",$sec,"\n"
  103.   }
  104. }
  105. kill ("TERM",$pid); 
Apr 17 '08 #1
Share this Question
Share on Google+
1 Reply


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Copy;
  5. my $InDir = "D:\\data\\eclipse\\workspace\\contents";
  6. my $file_count_old=-1;
  7. my $pid;
  8. die "cant fork $!\n" unless defined($pid=fork());
  9.  
  10.  if($pid) {
  11.  print "Here we go\n";
  12.  my $in=<STDIN>;
  13.    while ($in ne 'stop') {
  14.    $in=<STDIN>;
  15.    chomp $in;
  16.    print "$in is your input\n";
  17.        if($in eq 'stop') {
  18.        print "exiting.........\n";
  19.        }
  20.    }
  21.  }
  22.  else {
  23. LOOP1:
  24.   sleep 10;
  25.   {move_files()}
  26.  
  27. goto LOOP1;
  28.  
  29.  }
  30.  
  31. sub move_files {
  32.   my $SenDir = "D:\\data\\eclipse\\workspace\\contents\\sen";
  33.   my $SptDir = "D:\\data\\eclipse\\workspace\\contents\\spt";
  34.   my $WeaDir = "D:\\data\\eclipse\\workspace\\contents\\wea";
  35.   my $OutDir = "D:\\data\\eclipse\\workspace\\contents\\out";
  36.  
  37.   my @dirSen;  
  38.     opendir(DIR, $SenDir) or die "Unable to open dir $SenDir: $!\n";
  39.     @dirSen = sort readdir(DIR);
  40.     my $dir_sen_contents = $dirSen[2];
  41.     close(DIR);
  42.     #$dir_sen_contents is now the first file of sen directory
  43.  
  44.   my @dirSpt;  
  45.     opendir(DIR, $SptDir) or die "Unable to open dir $SptDir: $!\n";
  46.     @dirSpt = sort readdir(DIR);
  47.     my $dir_spt_contents = $dirSpt[2];
  48.     close(DIR);
  49.     #$dir_spt_contents is now the first file of spt directory
  50.  
  51.   my @dirWea;  
  52.     opendir(DIR, $WeaDir) or die "Unable to open dir $WeaDir: $!\n";
  53.     @dirWea = sort readdir(DIR);
  54.     my $dir_wea_contents = $dirWea[2];
  55.     close(DIR);
  56.     #$dir_wea_contents is now the first file of wea directory
  57.  
  58.   my $directory_count=0;
  59.   my $file_count=0;
  60.  
  61. opendir(DIR, $InDir);
  62.   LINE: while(my $FILE = readdir(DIR)) {
  63.     next LINE if($FILE =~ /^\.\.?/);
  64.     ## check to see if it is a directory
  65.     if(-d "$FILE"){
  66.       $directory_count++;
  67.     }
  68.     else {
  69.       $file_count++;
  70.     }
  71.   }
  72. closedir(DIR);
  73.  
  74. my $divisor = 9;
  75. my $remainder = $file_count % $divisor;
  76. my $FilesLeft = ($divisor - $remainder);
  77. my $tmp_sen_in ="$SptDir\\$dir_spt_contents";
  78. my $tmp_spt_in="$SenDir\\$dir_sen_contents";
  79. my $tmp_wea_in="$WeaDir\\$dir_wea_contents";
  80. my $tmp_sen_out="$OutDir\\$dir_spt_contents";
  81. my $tmp_spt_out="$OutDir\\$dir_sen_contents";
  82. my $tmp_wea_out="$OutDir\\$dir_wea_contents";
  83. my $file_out="temp";
  84. my $nav_date=substr $dir_spt_contents,1,6;
  85. (my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localtime(time);
  86. open (FILE, ">>$file_out") || die "Cannot write to $file_out\n";
  87.  
  88. if (($remainder==0) && ($file_count_old<$file_count)){
  89.   $file_count_old=$file_count;
  90.   print "$file_count_old\n";
  91.   move ($tmp_sen_in, $tmp_sen_out) or die "failed to move";
  92.   move ($tmp_spt_in, $tmp_spt_out) or die "failed to move";
  93.   move ($tmp_wea_in, $tmp_wea_out) or die "failed to move";
  94.   print "it worked! we moved it at :",$year+1900,"-",$mon+1,"-",$mday," ",$hour,":",$min,":",$sec,"\n\n";
  95.   while(<FILE>){
  96.     print "$dir_spt_contents$dir_sen_contents$dir_wea_contents.txt";
  97.   }
  98.   close (FILE);
  99.   rename "$file_out","$OutDir\\$file_out\_$nav_date" || die "Cannot update $file_out\n";
  100. }
  101. else {
  102.   print "They are currently uploading: $FilesLeft files left don't move em at ",$year+1900,"-",$mon+1,"-",$mday," ",$hour,":",$min,":",$sec,"\n"
  103.   }
  104. }
  105. kill ("TERM",$pid); 
Is there a question associated with this or just a code sharing exercise?

Regards,

Jeff
Apr 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.