1

My code is:

    Try
        For Each fi As IO.FileInfo In New IO.DirectoryInfo(BACKUP).GetFiles()
            If DateDiff(DateInterval.Month, fi.LastWriteTime, Now) > 6 Then
                fi.Delete()
            End If
        Next

I am just wondering why it won't delete a file from 02/04/2014. It is over 6 months ago but the file is still around. Any tips as to why this is?

7
  • Did this throw any errors?
    – OneFineDay
    Aug 6, 2014 at 14:52
  • No errors, it just won't delete the files that I need to be deleted after that 6 month mark Aug 6, 2014 at 15:00
  • It might be returning -6, try switching date1 and date2. Or use abs(datediff .... Aug 6, 2014 at 15:00
  • I tried switching the dates and it still didn't delete the file Aug 6, 2014 at 15:02
  • 2
    Have you checked what fi.LastWriteTime is returning? You say the file is from 02/04/2014 (I assume 4th Feb 2014 after reading other comments!), but is that the creation date, modified date or last accessed date?
    – Grim
    Aug 6, 2014 at 15:08

3 Answers 3

0

Both answers provide the cause, but neither provides a solution. Change this:

If DateDiff(DateInterval.Month, fi.LastWriteTime, Now) > 6 Then

To this:

If DateDiff(DateInterval.Month, fi.LastWriteTime, Now) >= 6 Then
0
0

From the MSDN documentation;

Larger Intervals. If Interval is set to DateInterval.Year, the return value is calculated purely from the year parts of Date1 and Date2. Similarly, the return value for DateInterval.Month is calculated purely from the year and month parts of the arguments, and for DateInterval.Quarter from the quarters containing the two dates.

So in this case, the result will be 6 - i.e. 08 minus 02 - which fails the test.

7
  • So that's saying that even though it is older than 6 months, because months is not over 6 it won't be deleted? Aug 6, 2014 at 15:19
  • 1
    @user3630473: Older than 6 months means at least 6 months old. "At least" is written as >= in any programming language. Aug 6, 2014 at 15:20
  • Exactly, as @Neolisk points out, the solution is to change your maths. It's ignoring the number of days. I think you can use the System.Math namespace and Timespan object to get the number of months in decimals... but I could be wrong...
    – Grim
    Aug 6, 2014 at 15:20
  • @Grim: TimeSpan does not have TotalMonths, because months are subjective and not consistent. Aug 6, 2014 at 15:21
  • 1
    I think last time I did something similar, I used the total number of days and compared to (30 days x 6 months = 180 days) and basically just deleted stuff if it was over 180 days old instead. Or of course, you can roll a whole new routine to return the number of months and days... etc...
    – Grim
    Aug 6, 2014 at 15:24
0
Dim Date1 As New Date(2014, 2, 4) ' fi.LastWriteTime
Dim Date2 As Date = Now ' 08/06/2014
Dim Diff As Long = DateDiff(DateInterval.Month, Date1, Date2)

Diff equals 6. > 6 equals at least 7. 6 is not higher than 6.

2
  • @TheBlueDog 02 >> month and 04 >> day?
    – user3453226
    Aug 6, 2014 at 15:06
  • Yes I am using US date, today is 08/06/2014 so it should be 6 months 2 days which I thought would delete the file from the records Aug 6, 2014 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.