File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes how to delete files datewise.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to delete files datewise.." Watch "how to delete files datewise.." New topic

how to delete files datewise..

unnikuttan kerala

Joined: Mar 23, 2006
Posts: 7

this is my first post in this forum.
i am trying to do a server routine check. in linux we have sub having one directory which stores all the log files of the server. i want to delete the files on 25th of every month. condition is that last 10 days file must be there..that means..delete the files created on 1-15 th of every month...

one idea is get system date ie 25th.then go 10 days behind and delete all the files upto that date..

how to create a java program..for that..
aslam parveez
Ranch Hand

Joined: Apr 25, 2006
Posts: 47
Yes you can get the current date and subtract 10 from it, then going thru the list of files in the specified folder get the lastModified date and if it is before the date that you have captured after subtracting 10 delete that file. Here is a sample code to do the same , i have considered "d:/temp" as the folder from where files are to be deleted.
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
You could also use the listFiles() method that returns an array of File objects - you don't need to create them in the loop then.
And you could also pass a FileFilter or FilenameFilter that only returned files before a given date to the listFiles() method. All you would need to do in your loop then is call File.delete()

aslam parveez
Ranch Hand

Joined: Apr 25, 2006
Posts: 47
Perfect Joanne!
I agree. Here's the link:
subject: how to delete files datewise..
It's not a secret anymore!