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

Perl script will not sort files from August to Dec 2007 for some reason

100+
P: 154
Hi I have a perl script, basically what it is suppose to do is check a folder with files. Now the files are checked using a timestamp with the command ls -l so the timestamp in this format is checked. Now what the script does is it checks the time stamp and creates a year folder if it does not exist and then creates a month folder if it does not exist and puts the respective files in the month folders. If the files are created this month then it leaves it in the folder it checks and moves others that are older to year, month folders.

Now I have tested the script and it works great . The script is doing what it suppose to but for some reason it is not processing files with timestamps from Aug 2007 to Dec 2007
It creates from Jan to July 2007 and all the other years and months but for 2007, for some reason these files disappear. Now I created an exception if it does nto go in any of the folders b put in exception and this does not happen.

Could somebody help me out in troublleshooting this or is this a bug. I am not sure where to check.


This also uses a sort_config.txt file
the file has info like this and you could make your own file if you want

So basically in the text file but info in a line like this
sourcepath destinationpath pathtologfile
e.g
/home/unix/sourcewherefilesare /home/destinationmonthsandyears /home/log20.txt

Make sure and separate the paths by a space and if you want to add comments
put # at the start of the line.

Here is the code
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. #Config File: sort_config.txt
  4. #Purpose: To sort files by create date and to archive them in respecitive 
  5. #folders by years and months.
  6. #Date: 01-17-2008
  7.  
  8. use File::Copy;
  9.  
  10. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  11.  
  12. $logdateTime2 = sprintf("%02d-%02d-%04d", $mon + 1, $mday, $year + 1900, $hour, $min, $sec);
  13.  
  14. # Get Current Date Time
  15. @datepcs = split(/\s+/, scalar localtime);
  16. $dateTime = "@datepcs[1]";
  17.  
  18. #open config file
  19. open(HAN, "sort_config.txt") || die " Unable to run date command $!";
  20.    while ($configline = <HAN>) 
  21.    {
  22.       $_ = $configline;
  23.  
  24.       # This next if statement is to remove lines from the array that start 
  25.       # with #
  26.       chomp($configline);
  27.  
  28.       # s/#.*//;            # ignore comments by erasing them
  29.       # next if /^(\s)*$/;  # skip blank lines
  30.       next if ($_ =~ /^(#|$)/);
  31.  
  32.       # Putting config file elements in an array
  33.       push @array1, [ split ];
  34.    }
  35. close HAN;
  36.  
  37. $i = 0;
  38. foreach (@array1) 
  39. {
  40.   # Processing configuration file
  41.   # Record Start Time for the Log
  42.   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  43.   $logdateTime = sprintf("%02d-%02d-%04d %02d:%02d:%02d", $mon + 1, $mday, $year + 1900, $hour, $min, $sec);
  44.   $sourcedir1 = $array1[$i][0];
  45.   $destdir1 = $array1[$i][1];
  46.   $log = $array1[$i][2];
  47.  
  48.   chomp($sourcedir1);
  49.   chomp($destdir1);
  50.   chomp($log);
  51.  
  52.   $line1= 'Start Date/Time | Files Processed | Duration';
  53.  
  54.   #IF log file does not exist create the log with the title header.
  55.   if (-e "$log") {}
  56.   else 
  57.   {
  58.     open(NEWLOG, "> $log") || die ("Could not NEWLOG open file $!");
  59.     print NEWLOG "$line1\n";
  60.     close (NEWLOG);
  61.   };
  62.  
  63.   ###print Header on LOG
  64.   open(INFILE2,"< $log") || die("Could not open INFILE2 File!");
  65.   $header=<INFILE2>;
  66.   chomp($header);
  67.  
  68.   if($header eq "$line1") {}
  69.   else
  70.   {
  71.     open(OUTFILE2, "> $log") || die ("Could not OUTFILE2 open file $!");
  72.     print OUTFILE2 "$line1\n";
  73.     print OUTFILE2 "$header\n";
  74.     while (<INFILE2>)
  75.     {
  76.       print OUTFILE2 "$_";
  77.     }
  78.     close OUTFILE2;
  79.   }
  80.   close INFILE2;
  81.  
  82.   ### Adding log info
  83.   open (LOG2, ">>$log") || die "error recording log $!";
  84.   &sort("$sourcedir1", "$destdir1");
  85.  
  86.   #Converting time and date formats from the subroutine below
  87.   my ($y, $m, $d, $hh, $mm, $ss) = (localtime)[5,4,3,2,1,0]; $y += 1900; $m++;
  88.   my $iso_now = sprintf("%d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $hh, $mm, $ss);
  89.  
  90.   #Calculating time difference
  91.   my $timeDiffStr = &timeDiff( date1 => $logdateTime, date2 => $iso_now );
  92.  
  93.   #Printing date time and Time difference and files processed to the log 
  94.   print LOG2 "$logdateTime | $rc | $timeDiffStr   ", "\n";
  95.   close (LOG2);
  96.  
  97.   ++$i;
  98. } # Close array1 loop
  99.  
  100.  
  101. # Start Subroutine to create folders and files in them.
  102. sub sort 
  103. {
  104.   #Run the date command to parse to get the year from the system.
  105.   $yearcmd = "date |";
  106.   open (ZING, "$yearcmd") || die " Unable to run date command $!";
  107.   $_ = <ZING>;
  108.   $_ = split;
  109.   $system_year = $_[5]; # Year showing up on the UNIX system
  110.   chomp($system_year);
  111.   $system_month = $_[1]; # Month showing up on the UNIX system
  112.   chomp($system_month);
  113.   chdir("$sourcedir1");
  114.   $test = "ls -l |";
  115.   open (INFO, "$test") || die " Unable to run list command $!";
  116.   @rows = <INFO>;
  117.   $rc = -1; # So result will not include the first line in the ls command
  118.   foreach (@rows) 
  119.   {
  120.     $_ = split; # Split the line output in elements
  121.     $year = "$_[7]"; # element to show the year
  122.     chomp($year);
  123.     $month = "$_[5]"; # Shows element of month
  124.     chomp($month);
  125.  
  126.    #print "This is the time of the file $year \n and this is the year on the 
  127.    #system **** $system_year ** \n";
  128.  
  129.    if ($year =~ m/\:/)
  130.    {
  131.      #Checking for files for this month
  132.      if ($year =~ m/\:/ && ($month eq $dateTime)) 
  133.      {}
  134.      elsif (-d "$destdir1/$system_year") #Check for files for this year only 
  135.      {
  136.        if (-d "$destdir1/$system_year/$system_month")
  137.        {
  138.          move("$_[8]", "$destdir1/$system_year/$system_month");
  139.        }
  140.        else 
  141.          {
  142.            # Create new dir if it does not exist
  143.            mkdir("$destdir1/$system_year/$system_month") or die "Can't open $srcdir: $!";
  144.            move("$_[8]", "$destdir1/$system_year/$system_month");
  145.          }
  146.      } 
  147.      else { 
  148.             #Create new dir if it does not exist
  149.             mkdir("$destdir1/$system_year") or die "Can't open $srcdir: $!";
  150.             mkdir("$destdir1/$system_year/$system_month") or die "Can't open $srcdir: $!";
  151.             move("$_[8]", "$destdir1/$system_year/$system_month");
  152.           }
  153.    }
  154.    else #else 1
  155.    {
  156.      if (-d "$destdir1/$year")
  157.      {
  158.        if (-d "$destdir1/$year/$month")
  159.        {
  160.          move("$_[8]", "$destdir1/$year/$month");
  161.        }
  162.        else 
  163.           {
  164.           mkdir("$destdir1/$year/$month") or die "Can't create sort dir $!";
  165.               move("$_[8]", "$destdir1/$year/$month");
  166.           }          
  167.      }
  168.      else 
  169.      {
  170.         # Create new dir if it does not exist
  171.         mkdir("$destdir1/$year") or die "Can't create sort dir $!";
  172.         mkdir("$destdir1/$year/$month") or die "Can't create sort dir $!";
  173.  
  174.         # Check if directory was created now
  175.         if (-d "$destdir1/$year/$month") 
  176.         {
  177.            move("$_[8]", "$destdir1/$year/$month");  
  178.         }
  179.         else
  180.         {
  181.            mkdir("$destdir1/exception") or die "Can't create sort dir $!";
  182.            move("$_[8]", "$destdir1/exception");
  183.         }
  184.  
  185.      }
  186.    } #close else 1
  187.      ++$rc; 
  188.  
  189.   }   # close loop 
  190.  
  191.    close(ZING);
  192.    close(INFO);
  193.  
  194. } # close subroutine
  195.  
  196.  
  197. # To calculate the time difference
  198. sub timeDiff (%)
  199. {
  200.   my %args = @_;
  201.   my @offset_days = qw(0 31 59 90 120 151 181 212 243 273 304 334);
  202.   my $year1  = substr($args{'date1'}, 0, 4);
  203.   my $month1 = substr($args{'date1'}, 5, 2);
  204.   my $day1   = substr($args{'date1'}, 8, 2);
  205.   my $hh1    = substr($args{'date1'},11, 2) || 0;
  206.   my $mm1    = substr($args{'date1'},14, 2) || 0;
  207.   my $ss1    = substr($args{'date1'},17, 2) if (length($args{'date1'}) > 16);
  208.   $ss1  ||= 0;
  209.   my $year2  = substr($args{'date2'}, 0, 4);
  210.   my $month2 = substr($args{'date2'}, 5, 2);
  211.   my $day2   = substr($args{'date2'}, 8, 2);
  212.   my $hh2    = substr($args{'date2'},11, 2) || 0;
  213.   my $mm2    = substr($args{'date2'},14, 2) || 0;
  214.   my $ss2    = substr($args{'date2'},17, 2) if (length($args{'date2'}) > 16);
  215.   $ss2  ||= 0;
  216.   my $total_days1 = $offset_days[$month1 - 1] + $day1 + 365 * $year1;
  217.   my $total_days2 = $offset_days[$month2 - 1] + $day2 + 365 * $year2;
  218.   my $days_diff   = $total_days2 - $total_days1;
  219.   my $seconds1 = $total_days1 * 86400 + $hh1 * 3600 + $mm1 * 60 + $ss1;
  220.   my $seconds2 = $total_days2 * 86400 + $hh2 * 3600 + $mm2 * 60 + $ss2;
  221.   my $ssDiff = $seconds2 - $seconds1;
  222.   my $dd     = int($ssDiff / 86400);
  223.   my $hh     = int($ssDiff /  3600) - $dd *    24;
  224.   my $mm     = int($ssDiff /    60) - $dd *  1440 - $hh *   60;
  225.   my $ss     = int($ssDiff /     1) - $dd * 86400 - $hh * 3600 - $mm * 60;
  226.  
  227.   #"$dd Tage $hh Std. $mm Min.";
  228.   "$hh Hours $mm Mins $ss secs";
  229. }
  230.  
Jan 25 '08 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
Can you narrow down the scope of the problem? Thats too much code to look through.
Jan 25 '08 #2

100+
P: 154
Ok from line 129 to 183 is where it actually checks the timestampa nd the year and month folder creations and where it moves the files. Now after posting this I did like some print statements and when it was creating 2007 Jan to Jul in the print statements in the loop it just stopped at July in the loop in print statement it did not print it created Aug to Dec. So this is very strange. Its like it just stopped but all the other years it runs fine for these.
Jan 25 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
I don't know, I am really not inclined to look over code written without using "strict". One small typo in a variable name could be the problem, or not. Using "stict" is very important, it helps a lot to insure your code is good if not the logic.

Your code is also severly tortured, it is written in a manner that makes it too hard to follow the flow and logic, all those if/elsif/if/else blocks are a nightmare to try and read and follow.

I humbly suggest an entire rewrite from scratch. This has to be much easier to accomplish than how you have gone about it.
Jan 25 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
I tried, but your script is so confusing I could not understand it. You really have to re-examine your approach and re-write the script. What you have right now is a nightmare of unnecessary complexity.
Jan 26 '08 #5

Post your reply

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