Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unlink files older than.......

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi Folks
How do I check if a file is older than 1 (or x minutes) minute.
If it is I want to remove it.
Like this:

if ('myfile' 'diffrent' than $min)
{
unlink("myfile.txt");
}


Thanks

/Fredrik
 
#!/usr/bin/perl

$min = 2;
$file = "genmove.htc";

checkFile($min, $file);


sub checkFile
{
my ($min, $file) = @_;
$min *= 60;
my $t = time;
my @tmp = stat($file);
if($tmp[9] > ($t - $min))
{
unlink($file);
return 1;
}
return 0;
}



this works for me. adam@aauser.com
 
by the way..

you need to change if($tmp[9] > ($t - $min))

to:

if($tmp[9] < ($t - $min))


i had it the other way around, because i was getting tired of deleting my files when i was testing it. adam@aauser.com
 
if you are on a *nix system, you can use a system call to the the find command to do this. Here's an example find command:
Code:
find /tmp \(-name &quot;*.txt&quot; -mtime +3\) -exec rm () \;
[\code]
This will remove any *.txt files in the /tmp directory that are older than 3 days. You should be able to modify it to suit your purposes.
 Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top