Results 1 to 5 of 5

Thread: [RESOLVED] Delete files 2 weeks old

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Resolved [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:
    1. Dim allPhotos() As String = IO.Directory.GetFiles(SYS_PHOTO_DIRECTORY)
    2.         Dim dt As DateTime
    3.         Dim fourteenDays as Integer = 14 'This is actually declared as a global variable as it will be used for other areas of my program.
    4.         For i As Integer = 0 To allPhotos.GetUpperBound(0) Step 1
    5.             dt = File.GetCreationTime(allPhotos(i)).ToLongDateString
    6.             If dt < Date.Now.AddDays(-(fourteenDays)) Then
    7.                 File.Delete(allPhotos(i))
    8.             End If
    9.         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

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Delete files 2 weeks old

    I would probably do it this way:

    VB Code:
    1. For i As Integer = 0 To allPhotos.GetUpperBound(0) Step 1
    2.             dt = File.GetCreationTime(allPhotos(i)).ToLongDateString
    3.             If dt.AddDays(fourteenDays) < Now Then
    4.                 File.Delete(allPhotos(i))
    5.             End If
    6.         Next

    This will delete photos that are 14 days old or older.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim twoWeeksAgo As Date = Date.Now.AddDays(-14)
    2.  
    3. For Each filePath As String In IO.Directory.GetFiles(SYS_PHOTO_DIRECTORY)
    4.     If IO.File.GetCreationTime([U]filePath[/U]) < twoWeeksAgo Then
    5.         IO.File.Delete(filePath)
    6.     End If
    7. Next filePath
    Last edited by jmcilhinney; Feb 1st, 2007 at 06:54 AM. Reason: Added the rather important underlined bit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    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
  •  



Click Here to Expand Forum to Full Width