• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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

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.
  • 2
  • 2
1 Solution
carlcomAuthor Commented:
PS  I am using absolute paths because the files to delete are far from the ASP page.
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
carlcomAuthor Commented:
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 & i.name)
      end if

It works nice!
thanks for posting and the grade :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now