wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Multiple Date Files Deletion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Multiple Date Files Deletion" Watch "Multiple Date Files Deletion" New topic
Author

Multiple Date Files Deletion

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
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?


Regards
Azrael Noor
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
i have achieved it with following way, just posting small code




Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Date Files Deletion