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
- ######################################
- # This will move files 1000 at a time#
- ######################################
- # Version 2. Now with error logging!#
- ######################################
- # Version 3. With email.Must install#
- # Mail-Sender module to use #
- ######################################
- # Verison 4. Including start and stop#
- # time to be based on real time not #
- # based on sleep timer. Must install #
- # DateCalc module to use #
- ######################################
- use File::Copy;
- use Mail::Sender;
- use Date::Calc qw(:all);
- #Source and destination directory defintion
- #priority 1 directory
- $dir_src_1="c:/test/source/1/";
- $dir_dst_1="c:/test/dest/1/";
- opendir(PRV_SRC_1, $dir_src_1) or die "Can't open $dir_src_1 : $!\n";
- opendir(PRV_DST_1, $dir_dst_1) or die "Can't open $dir_dst_1 : $!\n";
- #priority 2 directory
- $dir_src_2="c:/test/source/2/";
- $dir_dst_2="c:/test/dest/2/";
- opendir(PRV_SRC_2, $dir_src_2) or die "Can't open $dir_src_2 : $!\n";
- opendir(PRV_DST_2, $dir_dst_2) or die "Can't open $dir_dst_2 : $!\n";
- #priority 3 directory
- $dir_src_3="c:/test/source/3/";
- $dir_dst_3="c:/test/dest/3/";
- opendir(PRV_SRC_3, $dir_src_3) or die "Can't open $dir_src_3 : $!\n";
- opendir(PRV_DST_3, $dir_dst_3) or die "Can't open $dir_dst_3 : $!\n";
- #priority 4 directory
- $dir_src_4="c:/test/source/4/";
- $dir_dst_4="c:/test/dest/4/";
- opendir(PRV_SRC_4, $dir_src_4) or die "Can't open $dir_src_4 : $!\n";
- opendir(PRV_DST_4, $dir_dst_4) or die "Can't open $dir_dst_4 : $!\n";
- #Create/open log file and error log file
- open (LOG, ">>c:/test/dev/logfile.txt") or die "Can't open logfile: $!\n";
- open (ERROR, ">>c:/test/dev/errorlog.txt") or die "Can't open error logfile: $!\n";
- #Set sleep timer and number of files
- $nap=0; #initialize nap to 0
- $num_file=5; #number of files to move during each batch
- #set counter to 0
- $i=0; #num_file counter
- $j=0; #total number of files counter
- ($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
- $finish= Date_to_Time($f_year,$f_month,$f_day,$f_hour,$f_min,$f_sec); #converts finish time to seconds for comparison
- #get start time
- ($i_year,$i_month,$i_day,$i_hour,$i_min,$i_sec)=Today_and_Now();
- #change start time into number of seconds for comparison
- ($n_year,$n_month,$n_day,$n_hour,$n_min,$n_sec)=Today_and_Now();
- $now= Date_to_Time($n_year,$n_month,$n_day,$n_hour,$n_min,$n_sec);
- #loop will continue to run until it hits the finish time
- LINE: while ($now<$finish)
- {
- print "\nStarting at the top\n"; #error checking, making sure it's hitting top of loop
- #read files in private directory until there are no more files
- #program will run until counter hits num_file
- ##################################
- #Read each file in each directory#
- ##################################
- #priority folder 1
- while ((defined ($file1=readdir(PRV_SRC_1))) && $i<$num_file)
- {
- next if $file1 =~ /^\.\.?$/; # skip . and ..
- print "\nStarting pri 1"; #error checking
- #Move the currently selected file
- print LOG "\n",scalar localtime(), " Moving $file1 from $dir_src_1 to $dir_dst_1\n";
- move("$dir_src_1$file1","$dir_dst_1$file1") or warn error($!,$file1, $j); #if error, calls error sub function
- $i++;
- $j++;
- #program will run until counter hits num_file, then will sleep for 2hours before resetting counter and moving onto next file
- if ($i==$num_file)
- {
- bedtime($i,$j);
- }
- redo LINE; #attempt to get program to start at top of loop to check time and hopefully go back to priority one directory
- }
- #priority folder 2
- while ((defined ($file2=readdir(PRV_SRC_2)) && $i<$num_file))
- {
- next if $file2 =~ /^\.\.?$/; # skip . and ..
- #Move the currently selected file
- print LOG "\n",scalar localtime(), " Moving $file2 from $dir_src_2 to $dir_dst_2\n";
- move("$dir_src_2$file2","$dir_dst_2$file") or warn error($!,$file2, $j); #if error, calls error sub function
- $i++;
- $j++;
- #program will run until counter hits num_file, then will sleep for 2hours before resetting counter and moving onto next file
- if ($i==$num_file)
- {
- bedtime($i,$j);
- }
- redo LINE;
- }
- #priority folder 3
- while ((defined ($file3=readdir(PRV_SRC_3)) && $i<$num_file))
- {
- next if $file3 =~ /^\.\.?$/; # skip . and ..
- #Move the currently selected file
- print LOG "\n",scalar localtime(), " Moving $file3 from $dir_src_3 to $dir_dst_3\n";
- move("$dir_src_3$file3","$dir_dst_3$file") or warn error($!,$file3, $j); #if error, calls error sub function
- $i++;
- $j++;
- #program will run until counter hits num_file, then will sleep for 2hours before resetting counter and moving onto next file
- if ($i==$num_file)
- {
- bedtime($i,$j);
- }
- redo LINE;
- }
- #priority folder 4
- while ((defined ($file4=readdir(PRV_SRC_4)) && $i<$num_file))
- {
- next if $file4 =~ /^\.\.?$/; # skip . and ..
- #Move the currently selected file
- print LOG "\n",scalar localtime(), " Moving $file4 from $dir_src_4 to $dir_dst_4\n";
- move("$dir_src_4$file4","$dir_dst_4$file") or warn error($!,$file4, $j); #if error, calls error sub function
- $i++;
- $j++;
- #program will run until counter hits num_file, then will sleep for 2hours before resetting counter and moving onto next file
- if ($i==$num_file)
- {
- bedtime($i,$j);
- }
- redo LINE;
- }
- bedtime($i,$j); #tell program to sleep since no more files in directories
- #last; #uncomment if causing endless loop
- }
- #get end time
- $end= time();
- print LOG "\nFiles finished moving $j files on ", scalar localtime($end), "\n";
- #Close out the directories
- closedir PRV_SRC_1;
- closedir PRV_DST_1;
- closedir PRV_SRC_2;
- closedir PRV_DST_2;
- closedir PRV_SRC_3;
- closedir PRV_DST_3;
- closedir PRV_SRC_4;
- closedir PRV_DST_4;
- ####################################
- # End of main program #
- ####################################
- ####################################
- # Calculates nap time and current #
- # time for comparison to finish #
- ####################################
- sub bedtime
- {
- print "\nGoing to sleep now\n";
- print LOG "\nBreak on ", scalar localtime(), " after moving $i files and $j total files\n";
- #calculate how long program should sleep for
- #add time to initial start time if during first run or time from last end of sleep time
- ($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
- #find current time
- ($l_year,$l_month,$l_day,$l_hour,$l_min,$l_sec)=Today_and_Now();
- #calculate the difference and turn it into seconds
- ($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($l_year,$l_month,$l_day, $l_hour,$l_min,$l_sec,
- $i_year,$i_month,$i_day,$i_hour,$i_min,$i_sec);
- print LOG "\nWill nap for $Dd days, $Dh hours, $Dm minutes, $Ds seconds\n";
- $nap=($Dh*3600)+($Dm*60)+$Ds; #puts in seconds how long program should nap for based on difference to next nap period
- sleep $nap;
- $i=0;
- print "\nThat was a nice nap, back to work\n";
- print LOG "Starting transfer again on ", scalar localtime(), "\n\n";
- ($n_year,$n_month,$n_day,$n_hour,$n_min,$n_sec)=Today_and_Now();
- $now= Date_to_Time($n_year,$n_month,$n_day,$n_hour,$n_min,$n_sec);
- }
- ####################################
- # Begin error handling and email #
- # Will email log and error files #
- ####################################
- sub error
- {
- print ERROR "\n$file cannot be moved because of $! at ", scalar localtime(), "\n";
- print LOG "\n\nScript quit due to error after moving $j files, see error log time: ", scalar localtime(), "\n";
- ref($sender=new Mail::Sender{from=>'error@perl.com',
- smtp=>'ms3.bcapital.com'})
- or die "Error($sender):$Mail::Sender::Error\n";
- (ref($sender->MailFile({to => 'user@user.com',
- subject=> 'Danger Will Robinson',msg=>"Perl script error: $!\nPlease check error log\n\nHave a nice day!",
- file=>'logfile.txt,errorlog.txt'})) and print ERROR "Mail sent OK.\n")
- or die "$Mail::Sender::Error\n";
- $sender->Close();
- }