This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Date Files Deletion

 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Delete Multiple Files which are named with dates.

Like today is 27th, i have to delete all log files which are more than seven days old.

i have written code in java to change date
but how to delete multiple files in one instance?

have i to use only file.delete() and to make loops around it or is their any other method to do so?

 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have achieved it with following way, just posting small code




 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holy ****. I hope you never produce such code in production environments. It's completely illegible. Your variable names are horrible. I can figure out what "d", "m" and "y" do, "s" probably as well, but "g", "b", "sr" or "r"? And you do know that this isn't C, and you can declare variables in a for loop? That way you can move the declaration of "i" to the loop: I'm going to try to figure out what your code does, but I fear it's going to be harder than necessary. Properly named variables would make it soooo much easier.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to delete all files which are over seven days old, wouldn't it be easier to use the File method which tells you when the file was last updated, and see if that was over seven days ago?
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, I'm not going to figure this out.

However, I think I know a better way. Using File.listFiles() and File.lastModified() you can quite easily find all files older than 7 days*. A quick attempt, assuming all files are in the same folder:
You can improve the filter to perhaps use a regex to make sure the file names also have a proper format.


* The best way is to use Calendar, but in this case I doubt that 24L * 60L * 60L * 1000L * 7 will be a problem. (The L's are to ensure the result is a long; without they are treated as ints and you'll get overflow.)


@Paul: we had the same idea it seems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic