Results 1 to 4 of 4

Thread: [2005] Delete old files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    [2005] Delete old files

    Hi,

    My app lets you download backup files from the Internet and save them locally.

    I want the app to be able to automatically delete old backups though. What's the best way to do this?

    So if I allow the user to store five files named:

    backup-03-03-2009.zip
    backup-04-03-2009.zip
    backup-05-03-2009.zip
    backup-06-03-2009.zip
    backup-07-03-2009.zip

    When backup-08-03-2009.zip is downloaded it should delete backup-03-03-2009.zip and so on.

    Any tips/advice welcome!

    Thanks

    Simon

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2005] Delete old files

    Something like this, but obviously you should test it out:

    Code:
            Dim files() As String = IO.Directory.GetFiles("C:\Temp\backup")
            For Each f As String In files
                Dim filenamedate As String = f.Substring(f.Length - 14, 10)
                Dim dt As DateTime
                If Date.TryParse(filenamedate, dt) AndAlso DateDiff(DateInterval.Month, Now.Date, dt.Date) < -5 Then
                    IO.File.Delete(f)
                End If
            Next

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Re: [2005] Delete old files

    Ah - I see what you've done there. Unforuntately, the files won't always be called the same

    Can I get file attributes in vb.net? Eg the 'Created' date/time?

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2005] Delete old files

    Sure, you can use IO.File.GetCreationTime to get the creation time of a file.

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