473,412 Members | 3,860 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,412 software developers and data experts.

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

154 100+
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
4 2510
KevinADC
4,059 Expert 2GB
Can you narrow down the scope of the problem? Thats too much code to look through.
Jan 25 '08 #2
jonathan184
154 100+
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
4,059 Expert 2GB
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
4,059 Expert 2GB
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

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

Similar topics

17
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now...
4
by: FLEB | last post by:
I like PHP for its excellent inline integration into standard HTML files, but I like Perl for its quick-moving syntax and simpler data-processing. To resolve this deep-seated inner turmoil (oh, the...
16
by: squash | last post by:
a dumb question i had on my mind: Say you have a dynamically created web page . Isn't it more secure to write it in php since a visitor will not be able to tell it is dynamically created? But...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
5
by: Karyn Williams | last post by:
I am new to Pyton. I am trying to modify and understand a script someone else wrote. I am trying to make sense of the following code snippet. I know line 7 would be best coded with regex. I first...
6
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
7
by: pavanip | last post by:
Hi, I am newer to Perl script.I don't know about perl. I have to do one project in perl. That is already developed.But I have to do some modifications to that.Please tell me what are the...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.