Gossamer Forum
Home : General : Perl Programming :

Deleting after xx no. of days...

Quote Reply
Deleting after xx no. of days...
Hi. I am trying to write a postcard script. The way it works is to generate a .html file in a certain folder. The only function i am unsure of how to do is making this .html file get automatically deleted after xx number of days. Could someone please guide me as to how to do this Smile. I think what i may do is get the date, and then create a folder for all e-cards that are generated that day. Then when a set period of time is up it automatically deletes the folder (i don't know if this would need crontab, but if it doesn't it won't work). I know how to write to the file, but deleting the folder automatically is beyond me!!!

Thanks

Andy

Quote Reply
Re: Deleting after xx no. of days... In reply to
Why do you want to use static pages? I would recommend creating a database driven script that prints dynamic data from the database based on an access field like ID, UserID, and/or password.

Then you can simply REMOVE the record in the database after a certain number of days by writing another script (either CGI or shell) and then executing it via Cron.

Much easier...and also less intensive in terms of disk space and server resources (Memory, CPU) ---lot less to delete records out of a database then to check through a series of directories for files and then delete them.

I have this type of process outlined above working in my Postcard Site (using LINKS 2.0...soon to be using LINKS SQL)...

http://www.anthrotech.com/pc/

I also have a scheduler program installed that allows people to specify a future date when the card should be "sent" to the recipient.

Regards,

Eliot Lee
Quote Reply
Re: Deleting after xx no. of days... In reply to
I'm not very good with opening files and deleting certain lines. How would you suggest doing it?

Andy

Quote Reply
Re: Deleting after xx no. of days... In reply to
I posted an "Expirator Mod" for DBMAN in the DBMAN Customization Forum about six months ago. You could use it for LINKS 2.0. It is not really that hard to do...

All you really have to do is the following:

1) Define variables for the Removeby field and also for getting today's date.

2) Then compare the values of the Removeby field and today's date.

3) Then you open the database, and then create a foreach loop that checks each record for the Removeby date.

4) The actually deletion code looks like the following:

Code:

print DB $_, "\n";


Again, for complete codes, search the DBMAN Customization Forum for automatic delete Eliot.

Of course, this is much easier to do with MySQL since all you have to do is write a simple SQL command that deletes all rows that match today's date...no need for flock, file permissions, etc. Smile

Regards,

Eliot Lee
Quote Reply
Re: Deleting after xx no. of days... In reply to
returning to static pages...

umm.. to delete after a certain number of days.. you do this.. (in a crontab script)

my $days = 3;

opendir DIR, "/path/to/folder/with/postcards";
my @files = readdir (DIR);
closedir DIR;

foreach my $file (@files) {
next if ($file =~ /^\./);
my $date = (stat("/path/to/folder/with/postcards/$file"))[9];
if (time() - $date > $days * 86400) {
unlink ("/path/to/folder/with/postcards/$file");
}
}


there you go..

Jerry Su
widgetz sucks
Quote Reply
Re: Deleting after xx no. of days... In reply to
And to use system calls like unlink, you should execute the script behind a password protected directory. Wink

But, again, I think that using dynamic pages via one script is easier on your server and also less intensive in terms of disk space.

Regards,

Eliot Lee