Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code to check delete files if older that one week

Status
Not open for further replies.

bobalou

Programmer
Dec 8, 2003
17
US
I got help w/code to do autobackup of files using the windows scheduler. To keep my drive from getting cluttered I want to check the folder for files older that a certain number of days (e.g., 7, 10, 30) and delete them. Some files I want to keep longer in some folders (I have a series of loops to do the different backups). I haven't done vb in six years and those cells have died.

 
I got some help here and came up with this

Option Explicit

Dim fs, f, fc, fldr, fldrname, age

fldrname = "C:\Documents and Settings\cornettd\My Documents\Test\"

set fs = CreateObject("Scripting.FileSystemObject")
set fldr = fs.getfolder(fldrname)
set fc = fldr.files
for each f in fc
age = datediff("d", f.datelastmodified, now)
if age > 7 then
f.delete
end if
next


It seems to work. I would like to tweak it to loop through a list of folders (five at this time) and check each one rather than make a new copy for each separate folder.
 
Put your folder names into an array...
Code:
    fldrname = Array("C:\Junk\Test1","C:\Junk\Test2\","C:\Junk\Test3\","C:\Junk\Test4\","C:\Junk\Test5\")
    For Each folder In fldrname
      Set fs = CreateObject("Scripting.FileSystemObject")
      Set fldr = fs.getfolder(folder)
      Set fc = fldr.files
      For Each f In fc
        age = datediff("d", f.datelastmodified, now)
        if age > 7 then
          f.delete
        End if
      Next
    Next
 
Thanks, I was trying to make it harder than it was.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top