• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • 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")
objFSO.DeleteFile(Report_Filename)

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.
0
carlcom
Asked:
carlcom
  • 2
  • 2
1 Solution
 
carlcomAuthor Commented:
PS  I am using absolute paths because the files to delete are far from the ASP page.
0
 
bruintjeCommented:
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  

----------
bruintje
share what you know, learn what you don't
0
 
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
next

It works nice!
0
 
bruintjeCommented:
thanks for posting and the grade :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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