ASP - Need to delete files more then two days old.

Posted on 2006-06-01
Last Modified: 2010-04-07
Hello and thank you for reading.  In a particular folder, I want (in ASP) to delete all files that are older than two days.  Currently I can delete files by name only like (this code works):

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

I have tried getting the file properties, then I could get the names of old files and delete like above.  The code I have (this code gives an error "Microsoft VBScript runtime (0x800A01A8) Object required" on the last line):

        Set sh = CreateObject("Shell.Application")  
        set fl = sh.Namespace(folder)  
        for each f in fl.Items

folder in the second example is Report_Filename in the first example up to (including) the last "\", the same folder but no file name.  I don't think it'll be too difficult, but is urgent.
Question by:carlcom

    Author Comment

    PS  I am using absolute paths because the files to delete are far from the ASP page.
    LVL 44

    Accepted Solution

    Hi carlcom,

    not sure why you use the shell.appplication in your second script, i think you can use the filesystem object again like

    Dim FSO, oFol, oFils, oFil, dc

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oFol = FSO.GetFolder(myFolder)
      Set oFils = oFol.Files
      For Each oFil in oFils
         dc = oFil.DateCreated 'is date created
      Next oFil
      Set oFils = Nothing    

    Set oFol = Nothing
    Set FSO = Nothing  

    share what you know, learn what you don't

    Author Comment

    Thanks for that bruintje,

    Just for others, here is my finished code section:
    ' sPP is the physical folder path
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(sPP)

    Set fileList = f.Files
    For Each i in fileList
          if datediff("d", i.DateCreated, now()) > 2 then
                ' Delete the file
                fso.DeleteFile(sPP &
          end if

    It works nice!
    LVL 44

    Expert Comment

    thanks for posting and the grade :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now