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

Checking a folder again after it has been emptied

P: 1
have 4 folders that I watch and need to move files from to another
location. Three constraints:

-Finish time. Make sure the program stops transferring files at a
specific time
-Number of files transferred. Can only move a certain amount of files
per time period.
-Folders have a priority. The files have to be moved based on the
folder priority.

I have solved the first two constraints, but the third one is causing
problems. I am able to start with priority folder 1, but after it
empties the directory it will not check that folder again, even if I
put files back in it. I've tried a few different methods, but can't
seem to come up with something that will check that first folder
again. Below is the code. I think I've done a pretty good job
commenting, but I will be checking this if there are questions about
what the code does.

I feel like I hit a limitation of perl, please prove me wrong. Also,
thanks in advance for the help. Quick notes about the code, be sure
to change the finish time or it won't run. It will move 5 files and
sleep for 15 seconds, both of which can be changed.

Expand|Select|Wrap|Line Numbers
  1. ######################################
  2. # This will move files 1000 at a time#
  3. ######################################
  4. # Version 2.  Now with error logging!#
  5. ######################################
  6. # Version 3.  With email.Must install#
  7. # Mail-Sender module to use          #
  8. ######################################
  9. # Verison 4. Including start and stop#
  10. # time to be based on real time not  #
  11. # based on sleep timer. Must install #
  12. # DateCalc module to use             #
  13. ######################################
  14. use File::Copy;
  15. use Mail::Sender;
  16. use Date::Calc qw(:all);
  17.  
  18. #Source and destination directory defintion
  19.  
  20. #priority 1 directory
  21. $dir_src_1="c:/test/source/1/";
  22. $dir_dst_1="c:/test/dest/1/";
  23.  
  24. opendir(PRV_SRC_1, $dir_src_1) or die "Can't open $dir_src_1 : $!\n"; 
  25. opendir(PRV_DST_1, $dir_dst_1) or die "Can't open $dir_dst_1 : $!\n"; 
  26.  
  27. #priority 2 directory
  28. $dir_src_2="c:/test/source/2/";
  29. $dir_dst_2="c:/test/dest/2/";
  30.  
  31. opendir(PRV_SRC_2, $dir_src_2) or die "Can't open $dir_src_2 : $!\n"; 
  32. opendir(PRV_DST_2, $dir_dst_2) or die "Can't open $dir_dst_2 : $!\n"; 
  33.  
  34. #priority 3 directory
  35. $dir_src_3="c:/test/source/3/";
  36. $dir_dst_3="c:/test/dest/3/";
  37.  
  38. opendir(PRV_SRC_3, $dir_src_3) or die "Can't open $dir_src_3 : $!\n"; 
  39. opendir(PRV_DST_3, $dir_dst_3) or die "Can't open $dir_dst_3 : $!\n"; 
  40.  
  41. #priority 4 directory
  42. $dir_src_4="c:/test/source/4/";
  43. $dir_dst_4="c:/test/dest/4/";
  44.  
  45. opendir(PRV_SRC_4, $dir_src_4) or die "Can't open $dir_src_4 : $!\n"; 
  46. opendir(PRV_DST_4, $dir_dst_4) or die "Can't open $dir_dst_4 : $!\n"; 
  47.  
  48. #Create/open log file and error log file
  49. open (LOG, ">>c:/test/dev/logfile.txt") or die "Can't open logfile: $!\n";
  50. open (ERROR, ">>c:/test/dev/errorlog.txt") or die "Can't open error logfile: $!\n";
  51.  
  52.  
  53.  
  54. #Set sleep timer and number of files
  55. $nap=0; #initialize nap to 0
  56. $num_file=5; #number of files to move during each batch
  57.  
  58. #set counter to 0
  59. $i=0; #num_file counter
  60. $j=0; #total number of files counter
  61. ($f_year,$f_month,$f_day,$f_hour,$f_min,$f_sec)= (2007,02,12,19,53,00); #format: (YYYY,MM,DD,HH,mm,ss) HH must be 24h clock
  62. $finish= Date_to_Time($f_year,$f_month,$f_day,$f_hour,$f_min,$f_sec); #converts finish time to seconds for comparison
  63.  
  64. #get start time
  65. ($i_year,$i_month,$i_day,$i_hour,$i_min,$i_sec)=Today_and_Now();
  66.  
  67. #change start time into number of seconds for comparison
  68. ($n_year,$n_month,$n_day,$n_hour,$n_min,$n_sec)=Today_and_Now();
  69. $now= Date_to_Time($n_year,$n_month,$n_day,$n_hour,$n_min,$n_sec);
  70.  
  71. #loop will continue to run until it hits the finish time
  72. LINE: while ($now<$finish)
  73. {
  74.     print "\nStarting at the top\n"; #error checking, making sure it's hitting top of loop
  75.  
  76.     #read files in private directory until there are no more files
  77.     #program will run until counter hits num_file
  78.  
  79.     ##################################
  80.     #Read each file in each directory#
  81.     ##################################
  82.     #priority folder 1
  83.     while ((defined ($file1=readdir(PRV_SRC_1))) && $i<$num_file) 
  84.         {
  85.             next if $file1 =~ /^\.\.?$/;     # skip . and ..
  86.  
  87.             print "\nStarting pri 1"; #error checking
  88.             #Move the currently selected file
  89.             print LOG "\n",scalar localtime(), " Moving $file1 from $dir_src_1 to $dir_dst_1\n";
  90.             move("$dir_src_1$file1","$dir_dst_1$file1") or warn error($!,$file1, $j); #if error, calls error sub function
  91.             $i++;
  92.             $j++;
  93.  
  94.             #program will run until counter hits num_file, then will sleep for 2hours before resetting counter and moving onto next file
  95.             if ($i==$num_file) 
  96.             {
  97.                 bedtime($i,$j);
  98.  
  99.             }
  100.         redo LINE; #attempt to get program to start at top of loop to check time and hopefully go back to priority one directory
  101.  
  102.         } 
  103.  
  104.  
  105.  
  106.     #priority folder 2
  107.     while ((defined ($file2=readdir(PRV_SRC_2)) && $i<$num_file)) 
  108.         {
  109.             next if $file2 =~ /^\.\.?$/;     # skip . and ..
  110.  
  111.             #Move the currently selected file
  112.             print LOG "\n",scalar localtime(), " Moving $file2 from $dir_src_2 to $dir_dst_2\n";
  113.             move("$dir_src_2$file2","$dir_dst_2$file") or warn error($!,$file2, $j); #if error, calls error sub function
  114.             $i++;
  115.             $j++;
  116.  
  117.             #program will run until counter hits num_file, then will sleep for 2hours before resetting counter and moving onto next file
  118.             if ($i==$num_file) 
  119.             {
  120.                 bedtime($i,$j);
  121.  
  122.             }
  123.         redo LINE;
  124.         }
  125.  
  126.     #priority folder 3
  127.     while ((defined ($file3=readdir(PRV_SRC_3)) && $i<$num_file)) 
  128.         {
  129.             next if $file3 =~ /^\.\.?$/;     # skip . and ..
  130.  
  131.             #Move the currently selected file
  132.             print LOG "\n",scalar localtime(), " Moving $file3 from $dir_src_3 to $dir_dst_3\n";
  133.             move("$dir_src_3$file3","$dir_dst_3$file") or warn error($!,$file3, $j); #if error, calls error sub function
  134.             $i++;
  135.             $j++;
  136.  
  137.             #program will run until counter hits num_file, then will sleep for 2hours before resetting counter and moving onto next file
  138.             if ($i==$num_file) 
  139.             {
  140.                 bedtime($i,$j);
  141.  
  142.             }
  143.         redo LINE;
  144.         }
  145.  
  146.     #priority folder 4
  147.     while ((defined ($file4=readdir(PRV_SRC_4)) && $i<$num_file)) 
  148.         {
  149.             next if $file4 =~ /^\.\.?$/;     # skip . and ..
  150.  
  151.             #Move the currently selected file
  152.             print LOG "\n",scalar localtime(), " Moving $file4 from $dir_src_4 to $dir_dst_4\n";
  153.             move("$dir_src_4$file4","$dir_dst_4$file") or warn error($!,$file4, $j); #if error, calls error sub function
  154.             $i++;
  155.             $j++;
  156.  
  157.             #program will run until counter hits num_file, then will sleep for 2hours before resetting counter and moving onto next file
  158.             if ($i==$num_file) 
  159.             {
  160.                 bedtime($i,$j);
  161.  
  162.             }
  163.         redo LINE;
  164.         }
  165.  
  166. bedtime($i,$j); #tell program to sleep since no more files in directories
  167. #last;  #uncomment if causing endless loop
  168. }
  169.  
  170. #get end time
  171. $end= time();
  172.  
  173. print LOG "\nFiles finished moving $j files on ", scalar localtime($end), "\n";
  174.  
  175. #Close out the directories
  176. closedir PRV_SRC_1;
  177. closedir PRV_DST_1;
  178. closedir PRV_SRC_2;
  179. closedir PRV_DST_2;
  180. closedir PRV_SRC_3;
  181. closedir PRV_DST_3;
  182. closedir PRV_SRC_4;
  183. closedir PRV_DST_4;
  184.  
  185.  
  186. ####################################
  187. # End of main program              #
  188. ####################################
  189.  
  190. ####################################
  191. # Calculates nap time and current  #
  192. # time for comparison to finish    #
  193. ####################################
  194. sub bedtime
  195. {
  196.     print "\nGoing to sleep now\n";
  197.     print LOG "\nBreak on ", scalar localtime(), " after moving $i files and $j total files\n";
  198.  
  199.     #calculate how long program should sleep for
  200.     #add time to initial start time if during first run or time from last end of sleep time
  201.     ($i_year,$i_month,$i_day, $i_hour,$i_min,$i_sec) = Add_Delta_DHMS($i_year,$i_month,$i_day,$i_hour,$i_min,$i_sec,0,0,0,15);#last 4 determine nap length
  202.  
  203.     #find current time
  204.     ($l_year,$l_month,$l_day,$l_hour,$l_min,$l_sec)=Today_and_Now();
  205.  
  206.     #calculate the difference and turn it into seconds
  207.     ($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($l_year,$l_month,$l_day, $l_hour,$l_min,$l_sec,
  208.                 $i_year,$i_month,$i_day,$i_hour,$i_min,$i_sec);
  209.     print LOG "\nWill nap for $Dd days, $Dh hours, $Dm minutes, $Ds seconds\n";
  210.     $nap=($Dh*3600)+($Dm*60)+$Ds; #puts in seconds how long program should nap for based on difference to next nap period 
  211.  
  212.     sleep $nap; 
  213.     $i=0;
  214.     print "\nThat was a nice nap, back to work\n";
  215.     print LOG "Starting transfer again on ", scalar localtime(), "\n\n";
  216.     ($n_year,$n_month,$n_day,$n_hour,$n_min,$n_sec)=Today_and_Now();
  217.     $now= Date_to_Time($n_year,$n_month,$n_day,$n_hour,$n_min,$n_sec);
  218. }
  219.  
  220.  
  221. ####################################
  222. # Begin error handling and email   #
  223. # Will email log and error files   #
  224. ####################################
  225. sub error
  226. {
  227.     print ERROR "\n$file cannot be moved because of $! at ", scalar localtime(), "\n";
  228.     print LOG "\n\nScript quit due to error after moving $j files, see error log time: ", scalar localtime(), "\n";
  229.  
  230.     ref($sender=new Mail::Sender{from=>'error@perl.com',
  231.         smtp=>'ms3.bcapital.com'})
  232.         or die "Error($sender):$Mail::Sender::Error\n";
  233.  
  234.     (ref($sender->MailFile({to => 'user@user.com',
  235.              subject=> 'Danger Will Robinson',msg=>"Perl script error: $!\nPlease check error log\n\nHave a nice day!",
  236.             file=>'logfile.txt,errorlog.txt'})) and print ERROR "Mail sent OK.\n")
  237.                 or die "$Mail::Sender::Error\n";
  238.  
  239.  
  240.     $sender->Close();
  241. }
Feb 13 '07 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
the code runs? Seems to be riddled with errors or at least typos, like here:

Expand|Select|Wrap|Line Numbers
  1. $finish= Date_to_Time($f_year,$f_month,$f_day,$f_hour,$f_mi  n,$f_sec);
  2.  
what is that bare "n" in there for?
and here:

Expand|Select|Wrap|Line Numbers
  1. ($i_year,$i_month,$i_day,$i_hour,$i_min,$i_sec)=To  day_and_Now();
try and recode using "strict" and "warnings" and "diagnostics" pragmas to eliminate these possible errors/typos.
Feb 13 '07 #2

Post your reply

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