|
-
Jan 31st, 2007, 11:42 AM
#1
Thread Starter
Fanatic Member
[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?
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Jan 31st, 2007, 11:59 AM
#2
Fanatic Member
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.
-
Feb 1st, 2007, 03:50 AM
#3
Thread Starter
Fanatic Member
Re: Delete files 2 weeks old
Thanks looks a lot better.
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Feb 1st, 2007, 04:02 AM
#4
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
Last edited by jmcilhinney; Feb 1st, 2007 at 06:54 AM.
Reason: Added the rather important underlined bit.
-
Feb 1st, 2007, 05:23 AM
#5
Thread Starter
Fanatic Member
Re: [RESOLVED] Delete files 2 weeks old
Even better....thats what i was looking for.
Thanks

If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|