Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to delete files datewise..

 
unnikuttan kerala
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello..

this is my first post in this forum.
i am trying to do a server routine check. in linux we have sub directories..am 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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect Joanne!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic