Multiple File Deletions Based on Creation Dates

Discussion in 'ASP .Net' started by crjunk, Jan 3, 2005.

  1. crjunk

    crjunk Guest

    I have a folder named TXT24 on my web server that will contain multiple
    txt files. I want to add some code to the Session_Start event in my
    Global.aspx page that will scan all the files in the TXT24 folder and
    delete any text file that is over 24 hours old based on file's creation
    date. All the files in this folder will have random names, so I can't
    specify the file names in my code that need to be deleted.
    Can anyone give me some leads on how to do this?

    Thanks,
    crjunk
    crjunk, Jan 3, 2005
    #1
    1. Advertising

  2. "crjunk" <> wrote in message
    news:...
    > I have a folder named TXT24 on my web server that will contain multiple
    > txt files. I want to add some code to the Session_Start event in my
    > Global.aspx page that will scan all the files in the TXT24 folder and
    > delete any text file that is over 24 hours old based on file's creation
    > date. All the files in this folder will have random names, so I can't
    > specify the file names in my code that need to be deleted.
    > Can anyone give me some leads on how to do this?
    >
    > Thanks,
    > crjunk
    >
    Peter Rilling, Jan 3, 2005
    #2
    1. Advertising

  3. 1) Instantiate a DirectoryInfo object for your temp folder.
    2) Use DirectoryInfo.GetFiles to get a reference to all the files in the
    folder.
    3) Cycle through each file and use the FileInfo.CreationTime property to
    determine if it should be removed.
    4) Use the FileInfo.Delete to remove the file.

    Make sure IIS has access to that folder.

    "crjunk" <> wrote in message
    news:...
    > I have a folder named TXT24 on my web server that will contain multiple
    > txt files. I want to add some code to the Session_Start event in my
    > Global.aspx page that will scan all the files in the TXT24 folder and
    > delete any text file that is over 24 hours old based on file's creation
    > date. All the files in this folder will have random names, so I can't
    > specify the file names in my code that need to be deleted.
    > Can anyone give me some leads on how to do this?
    >
    > Thanks,
    > crjunk
    >
    Peter Rilling, Jan 3, 2005
    #3
  4. crjunk

    crjunk Guest

    Thanks for your help. I added a couple of excel files to the directory
    just for grins and I'm under the assumption that only text type files
    can be deleted, binary type files cannot be deleted. Here is what I
    came up with:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs)Handles Button1.Click

    Dim tmpFDir As System.IO.DirectoryInfo
    Dim tmpFArray As System.IO.FileInfo()
    Dim tDateTime As Date
    Dim i As Integer

    tmpFDir = New System.IO.DirectoryInfo("C:\TXT24")
    tmpFArray = tmpFDir.GetFiles()

    For i = 0 To tmpFArray.Length - 1
    tDateTime = DateAdd(DateInterval.Hour, 24,
    tmpFArray(i).CreationTime)
    If tDateTime < Now() Then
    tmpFArray(i).Delete()
    End If
    Next

    End Sub


    Thanks again!
    crjunk
    crjunk, Jan 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    858
    Rob Schieber
    Sep 30, 2005
  2. draeath
    Replies:
    10
    Views:
    414
    Dennis Lee Bieber
    Dec 4, 2010
  3. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    189
    Mark Schupp
    Aug 9, 2004
  4. Tim Sutherland

    RubyGarden deletions of RubyWeeklyNews

    Tim Sutherland, Feb 28, 2005, in forum: Ruby
    Replies:
    4
    Views:
    84
    Tim Sutherland
    Mar 1, 2005
  5. Ed Landau

    Getting file creation dates...

    Ed Landau, Jan 25, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    85
    Martin Herrmann
    Jan 28, 2004
Loading...

Share This Page