Purging files


The following chunk of code shows how to identify files over X days old using Java. With this, we can do things such as delete them if they’re over 30 days old for example.


// With this variable we can identify files over X days.
// In this example, anything over 30 days.
int daysBack = 30;

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, daysBack * -1);
long purgeTime = cal.getTimeInMillis();
System.out.format("purgeTime is now %d%n", purgeTime);

if (myfile.lastModified() < purgeTime) {
     // do something, maybe delete file?

This entry was posted in java and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s