Deleting Files Automatically

Discussion in 'ASP.NET / ASP.NET Core' started by pclcodesnet, Mar 1, 2010.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Looking for a solution that will delete uploaded files after they have been up on the server for more that two days.

    Any ideas or solutions?

    Thanks,

    Brad
     
  2. Code something like this into an aspx pages' code behind and then trigger page load as required from a scheduled task that you configure in the DASP scheduler.
     
  3. Thanks for the suggestion it looks perfect!

    Brad :)
     
  4. Scheduled Task - Delete Files

    To All,

    Has anyone written a .net web page that will delete files older than say two days using the Scheduled Task Manager?

    Thanks,

    Brad
     
  5. Bruce

    Bruce DiscountASP.NET Staff

    Try something like this

    Dim files() As FileInfo
    Dim file As FileInfo
    Dim di As DirectoryInfo = New DirectoryInfo(path)

    files = di.GetFiles("*.log", true)

    For Each file In files

    If DateDiff(DateInterval.Day, file.LastWriteTime, Now()) > 2 Then
    file.Delete()
    End If
    Next file
     
  6. Bruce,

    Do I create a console application in visual studio with your code and then call it with the Scheduled Task Manager?

    Thanks,

    Brad
     
  7. Bruce

    Bruce DiscountASP.NET Staff

    no.. put it in an aspx page and use the schedule task tool to call the page.
     
  8. Server Error in '/PCLcodes' Application

    Bruce,

    Thanks for the code! I creaded a blank DeleteFiles.aspx page and added your code. When I am testing it on my server I am getting the following Server Error below. Any Ideas?

    Server Error in '/PCLcodes' Application.
    --------------------------------------------------------------------------------

    Access to the path "125201014920.ini" is denied.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.UnauthorizedAccessException: Access to the path "125201014920.ini" is denied.

    ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

    To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

    Source Error:


    Line 27: For Each file As IO.FileInfo In directory.GetFiles
    Line 28: If DateDiff(DateInterval.Day, file.LastWriteTime, Now()) > 2 Then
    Line 29: file.Delete()
    Line 30: End If
    Line 31: Next file


    Source File: C:\Inetpub\wwwroot\PCLCODES.net\PCLcodes\DeleteFiles.aspx.vb Line: 29

    Stack Trace:


    [UnauthorizedAccessException: Access to the path "125201014920.ini" is denied.]
    System.IO.__Error.WinIOError(Int32 errorCode, String str) +393
    System.IO.FileInfo.Delete() +147
    PCLcodes.DeleteFiles.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\PCLCODES.net\PCLcodes\DeleteFiles.aspx.vb:29
    System.Web.UI.Control.OnLoad(EventArgs e) +67
    System.Web.UI.Control.LoadRecursive() +35
    System.Web.UI.Page.ProcessRequestMain() +750




    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:1.1.4322.2443; ASP.NET Version:1.1.4322.2407
     
  9. Bruce,

    I got the code working on my test server by adding the NETWORK SERVICE user to the folder and the files were deleted. Will I have the same problem up on the live server?

    Also how can I get the code to search in all subfolders located in my PCLcodesnet folder and delete files?

    Dim directory As New IO.DirectoryInfo("D:\web\pclcodesnet\htdocs\PCLcodesnet\")

    For Each file As IO.FileInfo In directory.GetFiles
    If DateDiff(DateInterval.Day, file.LastWriteTime, Now()) > 2 Then
    file.Delete()
    End If
    Next file
    End Sub

    Thanks,

    Brad
     
  10. Bruce

    Bruce DiscountASP.NET Staff

    You should be ok on the live server.

    I would, however, put the delete in a try and catch block in case some file is locked and cannot be deleted.
     
  11. How can I get the code listed below to search in all the subfolders located in my PCLcodesnet folder and delete files older than 2 days?

    Dim directory As New IO.DirectoryInfo("D:\web\pclcodesnet\htdocs\PCLcod esnet\")

    For Each file As IO.FileInfo In directory.GetFiles
    If DateDiff(DateInterval.Day, file.LastWriteTime, Now()) > 2 Then
    file.Delete()
    End If
    Next file
    End Sub

    Thanks,

    Brad
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page