in reply to Comparing Dates

I may be misunderstanding you, but if I guess right you want to READ every line in the text-files (which are like some sort of log-files it seems), extract the date in each of these lines, compare that date to today's date and if older than 1 month, delete that line in the file and save it in some archive file.

For date parsing and comparisons have a look at Date::Manip which is the Rolls Royce of date-modules, what it lacks in raw speed, it delivers in convenience.

Generally, you could do as follows:

  1. open your archive file for adding to it
  2. open the first of your files for reading
  3. read this file line by line
  4. check the date in each line and if older than one month write it to the archive file, else push it onto a stack for temporary storage
  5. At the end of the file; close it and reopen it for writing (this will clear the contents of this file!); else goto step 3
  6. now write your temporary stack to this file (hint: use foreach)
  7. close the file
  8. open your next file; if no more files remain: close your archive file; else goto step 3

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re^2: Comparing Dates
by bowei_99 (Friar) on Jan 28, 2006 at 01:06 UTC
    I think the original poster's code only gets files within the directory, not subdirectories. If the subdirectories are needed, then you could do a recursive solution, like -
    use File::Find; @ARGV = ('.') unless @ARGV; #start at . unless given # as command line argument sub ProcessFiles { &CheckAndFixFile($File::Find::name); } find(\&ProcessFiles, @ARGV); # find is recursive sub CheckAndFixFile { my $filename = shift @_; #Put code here to check if $filename meets criteria, and delete li +ne if needed. }

    The CheckAndFixFile subroutine is where you would use Date::Manip, or whichever Date module you like, to compare dates. It's easier than writing your own regular expression, as you did in your post, and it's code that's been tested and works.