472,799 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 software developers and data experts.

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

154 100+
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
  3. use File::Copy;
  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]);
  12. $dateTime = "@datepcs[1]";
  13. print $dateTime;
  15. #Source and Destination paths to change
  16. $sourceack = '/home/source;
  17. $ackdest = '/home/destination;
  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);
  27. chdir("$sourceack");
  28. $test = "ls -l |";
  29. open (INFO, "$test") || die " Unable to run list command $!"; 
  31. @rows = <INFO>;
  33. foreach  (@rows) {
  35.     $_ = split; # Split the line output in elements
  37.     $year ="$_[7]"; # element to show the year
  38.     chomp($year);
  39.     $month = "$_[5]";
  40.     chomp($month);
  41.     print $year ."\n";
  43.     if ("$year" =~ m/\:/) {
  45.         #Checking for files for this month             
  46.         if ("$year" =~ m/\:/ && ($month == $dateTime)) {}
  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: $!";
  67.             # Check if directory was created now
  68.             if (-d "$ackdest/$_[7]") {
  69.                 move("$sourceack/$_[8]", "$ackdest/$_[7]"); 
  70.             }
  71.         }
  72.     }
  73. }   # close loop
  75. close(INFO);
Oct 31 '07 #1
2 1661
4,059 Expert 2GB

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.

Oct 31 '07 #2
154 100+
ok thanks
Nov 1 '07 #3

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

Similar topics

by: FLOTServer | last post by:
Here's my problem: I run a gameserver that runs the game "Medal of Honor". On the game server is log file which contains all of the data from the players for that day (kills, deaths, etc...). I...
by: Johnny Knoxville | last post by:
I've added a favicon to my site (http://lazyape.filetap.com/) which works fine if you add the site to favourites the normal way, but I have some JavaScript code on a couple of pages with a link,...
by: Michael Klatt | last post by:
I need to design a container similar to a std::set, but where the stored objects may be modified via a non-const iterator. In some cases the modification will effect the sort order. Here's an...
by: B Maxey | last post by:
I am storing images in a database and need a way to browse those files. I can load the files into an image list or whatever the control needs, but I need a control. It doesn't seem that any of...
by: whitgurley | last post by:
I've searched the web as well as I can for a solution to this problem but have found nothing and just don't know enough about JavaScript to figure out what's going. What I'm trying to do is convert...
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
by: DjShifta | last post by:
Ok this really shouldn't be that hard...but I can not figure out whats' wrong for the life of me. I am brand new to "coding" and have thus far just built basic sites with Dreamweaver, but am going...
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
by: lllomh | last post by:
How does React native implement an English player?
by: Mushico | last post by:
How to calculate date of retirement from date of birth
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.