[RESOLVED] Delete files 2 weeks old
Hi guys,
I was just wondering what the best way to go about this was. I am using .net cf 2.0 and i need to delete any photos that were created over 2 weeks ago.
The code i am using at the moment is:
VB Code:
Dim allPhotos() As String = IO.Directory.GetFiles(SYS_PHOTO_DIRECTORY)
Dim dt As DateTime
Dim fourteenDays as Integer = 14 'This is actually declared as a global variable as it will be used for other areas of my program.
For i As Integer = 0 To allPhotos.GetUpperBound(0) Step 1
dt = File.GetCreationTime(allPhotos(i)).ToLongDateString
If dt < Date.Now.AddDays(-(fourteenDays)) Then
File.Delete(allPhotos(i))
End If
Next
and i was just wondering two things:
1) will this delete photos 13 days old or 14 days old
2)is this the best way to do this?
Re: Delete files 2 weeks old
I would probably do it this way:
VB Code:
For i As Integer = 0 To allPhotos.GetUpperBound(0) Step 1
dt = File.GetCreationTime(allPhotos(i)).ToLongDateString
If dt.AddDays(fourteenDays) < Now Then
File.Delete(allPhotos(i))
End If
Next
This will delete photos that are 14 days old or older.
Re: Delete files 2 weeks old
Thanks looks a lot better.
:wave:
Re: [RESOLVED] Delete files 2 weeks old
Why convert a Date to a String and back to a Date? You should keep binary Date objects as binary Date objects.
VB Code:
Dim twoWeeksAgo As Date = Date.Now.AddDays(-14)
For Each filePath As String In IO.Directory.GetFiles(SYS_PHOTO_DIRECTORY)
If IO.File.GetCreationTime([U]filePath[/U]) < twoWeeksAgo Then
IO.File.Delete(filePath)
End If
Next filePath
Re: [RESOLVED] Delete files 2 weeks old
Even better....thats what i was looking for.
Thanks
:thumb:
:wave: