473,289 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Checking a folder again after it has been emptied

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
1 1888
KevinADC
4,059 Expert 2GB
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

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

Similar topics

3
by: Rob Meade | last post by:
Hi all, I am allowing a user to create a directory within my application. I have them entering the name for the directory in a form. When the form is processed I initially check for any...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
6
by: Juggler | last post by:
Hi all I've written a program in VB.NET that allows the user to build quotes for installing shower enclosures. As part of the program, I've included a blank Access database. I've provided them...
8
by: vinesh | last post by:
I have sample Asp.Net Web Application project. Let me know how to keep the files related to this project (like the webform.aspx, WebForm1.aspx.vb, WebForm1.aspx.resx) in a separate folder within a...
2
by: tatemononai | last post by:
Ok, this is a very unique bug. I have only been able to find one other post related to this online, and everybody who responded misunderstood the problem. I am not trying to reference anything in...
2
by: patelxxx | last post by:
Checking the folder before actually creating the new folder . Can someone guide me in what 'Checks' do I need to do before I create a new folder? I know that one of the checks will be to see if...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
2
by: mindrage00 | last post by:
Hi! I am creating an image gallary where the images are numbered 1, 2, 3 etc. Users will be uploading the images so if i need to delete an image, I need the code to be able to check if that number...
1
by: Larry Bates | last post by:
I have a need to implement a drop folder upload mechanism for secure uploading of files to a server. At first glance this appears that it would be an easy application to write. Then I begin to...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.