File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Couldn't delete all files older than X days Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Couldn Watch "Couldn New topic

Couldn't delete all files older than X days

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Everyone,

I have downloaded this segment of code to delete all files including subfolders that are older than X number of days but have got stucked on line 25:

Below are the file listings of C:\\Test:

01/01/2011 12:14 PM <DIR> SubTestDirectory
22/02/2011 02:40 PM 1,086,518 TestOld.txt
12/05/2011 01:43 PM 1,720,223 TestNew.txt
2 File(s) 2,806,741 bytes

Directory of C:\Test\ SubTestDirectory

17/05/2011 12:14 PM <DIR> .
17/05/2011 12:14 PM <DIR> ..
22/02/2011 02:40 PM 1,086,518 subDirectoryTestOld.txt
12/05/2011 01:43 PM 1,720,223 subDirectoryTestNew.txt
2 File(s) 2,806,741 bytes
I expect files TestOld.txt, subDirectoryTestOld.txt and folder SubTestDirectory to be picked up but nothing was found.

Line 25 is crucial to determining whether the timestamp of all files and folder are over 30 days but it is not meeting this condition and I am not clear on whether the reason behind subtraction between System.currentTimeMillis() - (daysBack * 24 * 60 * 60 * 1000) to get purgeTime (line 22) either.

I am running JDK1.6.0_25 and Netbeans 7.0 on Windows XP platform.

Your assistance would be much appreciated.


Rob Spoor

Joined: Oct 27, 2005
Posts: 20050

Is your number of days over 24? If so, you have an overflow problem.

If the number of days is 25, the value will be 25 * 24 * 60 * 60 * 1000. The mathematical value is 2160000000. However, this is larger than Integer.MAX_VALUE, and therefore the value overflows to -12516353. As a result, the purge time will be in the future, and will never be met. Values larger than 25 will only make the problem worse; it's even possible the overflow is so bad that the multiplication results in a positive value again leading to perhaps purge all files.

The fix is easy:
1) declare daysBack as a long
2) cast daysBack as a long: 3) Use explicit long literals inside the calculation: For all three solutions, the fact that the first and/or second operand is a long turns the entire result into a long, allowing a value of 2160000000 without overflowing.

This is a common mistake. It happens so many times it's even described in the Java Puzzlers book.

How To Ask Questions How To Answer Questions
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Rob,

Thank you so much for having identified what appears to be a simple issue to a Java Guru can baffled a novice programmer quite many hours. Btw, can I confirm that both of the following suggestions 2 & 3 are identical:

I have used the latter one but would like to know whether they are equivalent.

Lastly, please let me know what is wrong with the questions that I have been posting so I won't make the same mistake in the future.

Thanks again,

I agree. Here's the link:
subject: Couldn't delete all files older than X days
It's not a secret anymore!