On Thu, Jun 22, 2006 at 04:45:25PM +0800, Khairul Azmi wrote: > Hi all, > I have this problem. There is a script that stores log files into folders > that follows structure of the following > > YYYY/MM/DD > > eg.. > > /var/log/projects/2006/06/20/8231.tgz > /var/log/project1/2006/06/21/1432.tgz > /var/log/projects/2006/06/22/1756.tgz > /var/log/projects/2006/06/23/1756.tgz > > Now I want to write a script that would erase all logs that has been stored > except for the log for the current day. For the above example, since today > is 23 June 2006, the code will erase log files in folders 2006/06/20/ and > 2006/06/21/ only. Can somebody give ideas on how to do this. This script is > to be ran once a day.
If it's run every day, you could conceivably decrement the date by one every day and delete the file corresponding with the date you get by decrementing. This isn't a very fault tolerant solution, but it's a simple one. The first time you run it, you'll have to delete anything before the current date, of course. To delete all previous dates, in case you end up with more than one, you could use a for loop or similar increment/decrement looping structure to delete files starting with a first decremented date and stop when you can't find a file that matches a decremented date value. You may want to search CPAN for a date-handling module that suits your purposes so you don't have to write all the date-handling logic of your program yourself. You've already received a suggestion of what to use for handling file operations in another reply to your request. Whatever you do, I recommend of course that you use the strict and warnings pragmas, and test the heck out of it on sample files before using it on the real things if you care about whether you lose your data during testing. -- CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] Ben Franklin: "As we enjoy great Advantages from the Inventions of others we should be glad of an Opportunity to serve others by any Invention of ours, and this we should do freely and generously." -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>