Delete all files not modified in last ten days

1.1k views Asked by At

I have a folder where I keep log files and I have a job scheduled to run each day to delete old logs.

What's a good way to delete old log files?

Currently, I loop through the directory to read files

File[] listOfFiles = srcFolder.listFiles(fileNameFilter); // this makes an array of files which match a filter I created
for(int i=listOfFiles.length-1; i>0; i--){ // leave at least one file

    // listOfFiles[i].lastModified() - in last ten days?
    if( what condition ?){
        try {
            listOfFiles[i].delete();
        } catch(Exception e){

        }
    }
}

I'm thinking

 if(listOfFiles[i].lastModified() < tenDaysAgoMS) // where tenDaysAgoMS is the milliseconds ten days ago, but how to implement?
4

There are 4 answers

0
spongebob On BEST ANSWER

First, you have to subtract the last-modified property to the current time. See Calendar.getTimeInMillis() and File.lastModified().

Then, you can convert days to milliseconds by multiplying them to 1000 * 60 * 60 * 24.

  1. 1000ms = 1s

  2. 60s = 1m

  3. 60m = 1h

  4. 24h = 1d


long now = Calendar.getInstance().getTimeInMillis();
long oneDay = 1000L * 60L * 60L * 24L;
long tenDays = 10L * oneDay;

File log = listOfFiles[i];
long diff = now - log.lastModified();
if (diff > tenDays) {
    log.delete();
}
3
usha On

you could do something like

if( (System.currentTimeMillis() - listOfFiles[i].lastModified()) > 10 * 24 * 60 * 60 * 1000){
}
2
Olivier Grégoire On

Use the tools at your disposal. It's much clearer than playing with milliseconds and stuff like that. I give an answer with Java's Calendar because I get that you shouldn't need any external library like Joda Time, since the task is quite easy. If you have Java 8, use the java.time API.

Calendar tenDaysAgo = Calendar.getInstance();
tenDaysAgo.add(Calendar.DAY_OF_MONTH, -10); // yeah, Calendar is really not a good API.

File[] listOfFiles = srcFolder.listFiles(fileNameFilter);
for(int i = listOfFiles.length - 1; i > 0; i--){ // leave at least one file
    File file = listOfFiles[i];
    Calendar fileDate = Calendar.getInstance();
    fileDate.setTime(new Date(file.lastModified()));
    if (fileDate.before(tenDaysAgo)) {
        try {
            file.delete();
        } catch (IOException e) {
            // handle the error
        }
    }
}
1
Basil Bourque On

More Precise

How specific is your requirements? The other answers are good enough for approximately ten days. But if you mean something more precise such as exactly the ten full days prior to today, then you need to use either the Joda-Time library or the new java.time package built into Java 8 (inspired by Joda-Time).

Get the first moment of the day by calling withTimeAtStartOfDay. This is usually 00:00:00.000 but not always.

How important is time zone. If omitted, you get the JVM’s current default time zone. Generally better to specify the desired time zone. Perhaps you want UTC for consistency.

Joda-Time

Example code in Joda-Time 2.5.

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ); // or DateTimeZone.UTC
DateTime now = DateTime.now( zone );
DateTime then = now.minusDays( 10 ).withTimeAtStartOfDay();
long millisSinceEpoch = then.getMillis();  // If needed to compare to files.