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

Need some help where I am going wrong with my sorter script

100+
P: 154
Hi basically what i want this script to do is
In a particular dir there are alot of files from today back to 1999 or earlier.
So I am trying to have the script search this dir and sort the files by create date of the file and put in dirs by year and the only data iw ant left in the source dir is this months only and when moving to next month i want only that month and so forth. .
for e.g is the create date has a year of 2004 I want it to put the file in a folder called 2004 if the folder does not exist create the folder and move the file there.

The other part in unix when you run ls -l , this is what i am using to create the array then split by spaces.

So when the files are create by this year. Sometimes in the year column it would show the time instead meaning it was created this year.

So now I want to break it dow where i am comparing the system month with month of the file and if they are the same and time is in that column leave the files the current dir. Now if the time is in column and the month is not the same put in the folder of that year. So when i move to another month only the files fromt hat month will stay and the rest are moved to the year folders.

This is what created. The problem i am getting is the files are moved to the destination folder where the year folders are suppose to be created. they are created but the files are not going in the folders and the source folder is gone after i runt he script. Ic hecked my paths and i am not seeing anything wrong. Hopefully somebody could help me please.


Here is an example how two records look on the screen. when i run the ls -l command.
-rw-r----- 1 bridge busr 1717 Oct 30 15:01 file2.txt

-rw-r----- 1 bridge ausr 1384 Mar 2 2004 file1.txt

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use File::Copy;
  4.  
  5. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  6. $logdateTime = sprintf("%02d-%02d-%04d  %02d:%02d:%02d", $mon + 1, $mday, $year + 1900, $hour, $min, $sec);
  7. $logdateTime2 = sprintf("%02d-%02d-%04d", $mon + 1, $mday, $year + 1900, $hour, $min, $sec);
  8. # Get Current Date Time
  9. @datepcs = split(/\s+/, scalar localtime);
  10. #printf("%s %s", @datepcs[1,2]);
  11.  
  12. $dateTime = "@datepcs[1]";
  13. print $dateTime;
  14.  
  15. #Source and Destination paths to change
  16. $sourceack = '/home/source;
  17. $ackdest = '/home/destination;
  18.  
  19. #Run the date command to parse to get the year.
  20. $yearcmd = "date |";
  21. open (CMD, "$yearcmd") || die " Unable to run date command $!"; 
  22. @dd = <CMD>;
  23. $system_year = "$dd_[5]"; # Year showing up on the UNIX system
  24. chomp($system_year);
  25. close(CMD);
  26.  
  27. chdir("$sourceack");
  28. $test = "ls -l |";
  29. open (INFO, "$test") || die " Unable to run list command $!"; 
  30.  
  31. @rows = <INFO>;
  32.  
  33. foreach  (@rows) {
  34.  
  35.     $_ = split; # Split the line output in elements
  36.  
  37.     $year ="$_[7]"; # element to show the year
  38.     chomp($year);
  39.     $month = "$_[5]";
  40.     chomp($month);
  41.     print $year ."\n";
  42.  
  43.     if ("$year" =~ m/\:/) {
  44.  
  45.         #Checking for files for this month             
  46.         if ("$year" =~ m/\:/ && ($month == $dateTime)) {}
  47.  
  48.         #Check for files for this year only
  49.         elsif (-d "$ackdest/$system_year") {
  50.             move("$sourceack/$_[8]", "$ackdest/$system_year");   
  51.         }
  52.         else { 
  53.             # Create new dir if it does not exist
  54.             mkdir("$ackdest/$system_year") or die "Can't open $srcdir: $!";
  55.             move("$sourceack/$_[8]", "$ackdest/$system_year");
  56.         }
  57.     }
  58.     else {
  59.         # IF the directory exists
  60.         if (-d "$ackdest/$year") {
  61.             move("$sourceack/$_[8]", "$ackdest/$year"); 
  62.         }
  63.         else { 
  64.             # Create new dir if it does not exist
  65.             mkdir("$ackdest/$year") or die "Can't open $srcdir: $!";
  66.  
  67.             # Check if directory was created now
  68.             if (-d "$ackdest/$_[7]") {
  69.                 move("$sourceack/$_[8]", "$ackdest/$_[7]"); 
  70.             }
  71.         }
  72.     }
  73. }   # close loop
  74.  
  75. close(INFO);
  76.  
Oct 31 '07 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
jonathan,

I'm sorry mate, but I'm not going to help with an existing script that long that has not been written using "strict" and one that contains blatant syntax errors like here:

#Source and Destination paths to change
$sourceack = '/home/source; <--- no terminating quote
$ackdest = '/home/destination; <--- no terminating quote

If you want to rewrite it with "strict" and clean up syntax errors I will see if I can help you.

Kevin
Oct 31 '07 #2

100+
P: 154
ok thanks
...............
Nov 1 '07 #3

Post your reply

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