/dev/trash has asked for the wisdom of the Perl Monks concerning the following question:

I am working in a Windows environment but I am pretty sure this is an OS_independent question. I want to check to see if a group of files were modified on the present date. If they were modified before today I want them deleted, else keep them. I know about stat() and localtime but the best I could come up with was when the file was modified. Any suggestions would be great.
  • Comment on How do I determine if a file was modified today?

Replies are listed 'Best First'.
Re: How do I determine if a file was modified today?
by fs (Monk) on Jun 02, 2001 at 10:19 UTC
    You've got the mtime, so you're already halfway there. What you do with the mtime depends on what you mean by "modified today".

    If you mean within the last 24 hours, that's easy. Since the mtime is second ssince the epoch,

    # 24 hours * 60 minutes/hour * 60 seconds/minute $threshhold = 86400; $currenttime = time(); if( ($currenttime - $mtime) > $threshhold){ # file was modified within 24 hours }
    Or, if you mean was the file modified after the previous 12am, you have to calculate from the start of the caldendar day with something like this
    if( $mtime > ($currenttime - ($currenttime % 86400)){ # file was modified this calendar day }
Re: How do I determine if a file was modified today?
by mpolo (Chaplain) on Jun 02, 2001 at 10:24 UTC
    Well, stat() gives you epoch seconds since last modification. Then you'll need to get the epoch seconds for 12:00 am on the present date -- use localtime() to get the day, month, and year, then pass those back to timelocal() with hours, minutes, and seconds set to zero to get the time of midnight. Now any file for which the stat() time is greater than the time of midnight was made today.
    use Time::Local; my $mtime=(stat("filename.extension"))[13]; my ($day, $month, $year)=(localtime())[3,4,5]; my $midnight=timelocal(0,0,0,$day,$month,$year); unlink ("filename.extension") if ($mtime>$midnight);
Re: How do I determine if a file was modified today?
by ton (Friar) on Jun 02, 2001 at 12:45 UTC
    If by "today" you mean "in the last 24 hours", try this:
    if (1 > -M $filename) { # Your code here }

    Be bloody, bold, and resolute; laugh to scorn
    The power of man...
      Nice and concice. If the poster meant "in the last 24 hours" to mean this calendar day, let's combine this concise answer with some of the others above:
      if ( 1 > -M $file and (localtime((stat _)[9]))[3] == (localtime)[3] ) { # modified "in the last 24 hours" }
      Beware of small, subtle bugs around Daylight Savings time.
Re: How do I determine if a file was modified today?
by Desdinova (Friar) on Jun 02, 2001 at 12:36 UTC
    Just a thought if you want to base it on the date (ie the 25th) you can call localtime and give it the mtime to work with. You get the mtime into a var then call localtime using that var. Then use localtime(time) to determine the current date and comapre them the code below runs on Win 98 SE. I hope that this is what you are looking for<
    #!/usr/bin/perl -w use strict; my $filename = 'Temp.txt'; #get stats my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, +$blksize,$blocks)= stat($filename); #get current time and date my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(tim +e); #convert modtime to local format my($mod_sec,$mod_min,$mod_hour,$mod_mday,$mod_mon,$mod_year,$mod_wday, +$mod_yday,$mod_isdst)=localtime($mtime); #compare current date to modified date if (($mod_mday == $mday) && ($mod_mon == $mon) && ($mod_year == $year) +) { #Do whatever if modified today print "$filename modified today\n" } else { #DO things if not modified today print "$filename not modified today\n"; }
Re: How do I determine if a file was modified today?
by rchiav (Deacon) on Jun 02, 2001 at 18:05 UTC
    You should also test against "-C". -M will catch if the file is modified, but what if someone copies up a file right before the script is run? It will give the time since modified, but not sice it was copied up. I think it's better to test both..

    I use..

    $age = int( -M ) < int( -C ) ? int( -M ) : int( -C );