bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Could somebody help me, how to delete file on last 3 month (based on file date ) ? ....
  • Comment on How to delet file on last 3 month ? ....

Replies are listed 'Best First'.
Re: How to delet file on last 3 month ? ....
by b10m (Vicar) on Jan 13, 2004 at 05:16 UTC
    "how to delete file on last 3 month (based on file date ) ?"

    Unfortunately, you're not really specific of exactly *which* "file date" you want, for there are three sorts of times for each file, namely:

    • atime
    Access time. This is the time that the file was last opened
    • ctime
    Change time. This time changes when you change ownership, permissions and what not (once again, it's not creation time)
    • mtime
    Modification time. This specifies when the file was last changed (the contents of the file).

    But, you're lucky, 'cause with File::Finder, you can look for all of the above (you probably want to check for mtime). Once you found the right files, you can use unlink to delete them.

    --
    b10m
Re: How to delet file on last 3 month ? ....
by bh_perl (Monk) on Jan 13, 2004 at 05:22 UTC
    Based on some cases here, I was found this solution....
    my $tf = (stat("$fdir/$file"))[9]; my $tt = time() - (86400 * 90); if ( $tf < $tt ) { unlink "$fdir/$file"; }
    But, If you have some better ideas, Pleased advised me, I am very appreciated about that.... Thank you,
      If 3 months from the starting time of your program will work (instead of 3 months from the moment you are considering deleting the file), you can use -M, which subtracts script-start-time and converts to days for you:
      unlink "$fdir/$file" if 90 > -M "$fdir/$file";
      See perldoc -f -M
Re: How to delet file on last 3 month ? ....
by greenFox (Vicar) on Jan 13, 2004 at 05:39 UTC

    It would be nice if you specified things like which OS you are using and if the solution is part of a larger Perl program or you just want to delete files.

    A non Perl solution:  find /some/path -mtime +90 -exec rm {} \; or maybe you want to do it with Perl? See perlfunc:stat

    --
    Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Re: How to delet file on last 3 month ? ....
by dominix (Deacon) on Jan 13, 2004 at 11:28 UTC
    directly from find2perl
    #! /usr/bin/perl -w use strict; use File::Find (); use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; File::Find::find({wanted => \&wanted}, '.'); exit; sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && (int(-M _) >= 90) && unlink $name; }
    warning untested.
    update: this example delete file older than 3 months, not within 3 months. just play with int(-M _)>= X AND int(-M _)<= Y for between time slice (X,Y in days).
    --
    dominix
Re: How to delet file on last 3 month ? ....
by TomDLux (Vicar) on Jan 13, 2004 at 08:42 UTC

    Except 90 days is not 3 months, except in a non-leap-year January/February/march. Depending on your definition of three months, you might want to take a look at Date::Calc.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      "Except 90 days is not 3 months"

      You are right but I imagine that in any application where a time specification as vague as "3 months" is acceptable, 90 days is in all likelihood a good enough approximation :)

      --
      Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Re: How to delet file on last 3 month ? ....
by pg (Canon) on Jan 13, 2004 at 05:54 UTC

    If you are on unix, take a look at find command.

    Even if you are on windows, with Perl, you can still enjoy the power that find command provides. Check out find2perl.bat in your perl bin directory.