473,756 Members | 5,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

154 New Member
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 troublleshootin g 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/sourcewherefile sare /home/destinationmont hsandyears /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 2532
KevinADC
4,059 Recognized Expert Specialist
Can you narrow down the scope of the problem? Thats too much code to look through.
Jan 25 '08 #2
jonathan184
154 New Member
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 Recognized Expert Specialist
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 Recognized Expert Specialist
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
8294
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 with form elements that turn out to be arrays that have to be compared with one another! I have one form element, languages, a checkbox group. Beside each checkbox is a dropdown, proficiency (which will become proficiency alongside languages)....
4
3152
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 drama) I've been trying to think of good ways to get Perl code to run inline in a PHP script. Here are a few of my ideas. If anyone has any further ideas, resources, or knows of someone else who's solved this already, please do tell... 1.) The...
16
2515
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 if you write it in perl, you have to put it in your cgi-bin which automatically means it's a dynamically generated page? security by obscurity . Is this true or hardly means anything? thx!
0
9745
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. I need it to be compiled with threads. Anyone have any wisdom on how best to do this? Here's a transcript of my latest attempt. It's long; you might want to skip to the bottom, where I try "make" and the fatal errors start happening.
5
2526
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 would like to understand what was coded originally. thelistOut looks like a hash to me (I'm more familiar with perl). Perhaps someone could translate from perl to python for me - not in code but just in concept. Here is the code. This script...
6
3012
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 for one of the Pear modules for creating XML and it didn't look like much. I've used Perl XML:Twig and I have the impression that there is more Perl stuff available as well as the Perl stuff being well documented as I have a Perl DBI book, a Perl...
21
34434
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
7
1689
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 components I have to install in our system to run the perl script.My operating system is Windows XP. And how to compile and run the script.Here my major problem is script is not opening.Is there any other alternative to open the script.That file is in .cgi...
1
47480
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 on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
9454
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10028
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9836
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6533
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.