Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Purge Old Log Files

Status
Not open for further replies.

ravioli

Programmer
Mar 30, 2004
39
US
Hello All,

I want to purge all log files in a directory that are over 60 days old. Is there a Java class that will tell me how old a file is or perhaps a "created date?" I have been looking and can't find one. Hint I do have the YYYYMMDDHHMM as part of the file's name. Can I turn this into a date?

I could also go another route where I subtract from todays date. It seems like if I go this route that I will need to do a calculation of time in miliseconds from some day in 1970.

Which is the best route? Or is there another?

Thanks for any help,
Bradley
 
Look at the lastModified() method of the File class. It's not the created date but it might work for you.

_________________
Bob Rashkin
 
Bong,

Let me look at this it might work just fine. I will let you know how it turns out.

Bradley
 
Failing that, you could use a SimpleDateFormat to turn that part of the file's name you quoted as being in YYYYMMDDHHMM format into a Date object:-
Code:
DateFormat df = new SimpleDateFormat("yyyyMMddhhmm");
String datePortion = ... extract date portion from filename
Date fileDate = df.parse(datePortion);

Note that the parse method throws a ParseException which you will need to catch. DateFormat, SimpleDateFormat and ParseException are all found in the java.text package.

Tim
 
Bong,

I was able to use your method. Here is the final product. It figures out if files are older than 60 days and deletes them if they are (thanks again). Works great:

package geacProcessPackage;

import java.io.*;

final class cleanArchive {

static boolean cleanFiles(File directory, BufferedWriter logOut)
throws Exception {
if (logOut != null) {
logOut.write(" CLEANFILES -- " + directory
+ " will be cleaned\r\n");

// CHG-04042007
long modifiedTime;
long t_difference;
long t_60_days_ago = System.currentTimeMillis() - 5184000000L;
// end CHG-04042007

if (directory.isDirectory()) {
String[] children = directory.list();

for (int i = 0; i < children.length; i++) {
File fileToDelete = new File(directory, children);

// CHG-04042007
modifiedTime = fileToDelete.lastModified();
t_difference = modifiedTime - t_60_days_ago;

if (t_difference < 0) {
logOut.write(" CLEANARCHIVE -- Deleting file "
+ children + ".\r\n");
fileToDelete.delete();
}
}
}
}

return true;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top