Bookmark

Donate

Subscribe

Subscribe to our RSS feed

Subscribe to our RSS feed
Email Subscription

Enter your Email ID:

Delivered by FeedBurner

Categories

Links

How to automatically delete folders that are n days old?

Published: Dec 01, 2005
Send your feedback

Question

How to automatically delete the folders that are more than 2 days old? I have the following directory structure, to store certain reports for each day:

  • D:\Reports\20051102
  • D:\Reports\20051103
  • D:\Reports\20051104
  • D:\Reports\20051105
  • D:\Reports\20051106

I want a script that'll delete the folders that are more than 2 days old from now. I should be able to schedule this script to run daily, as well.

Scripting - DeleteFolder method

Here is a simple script that'll accomplish what you need. Copy this to a Notepad, and save as "DelFolder.VBS" with quotes, and then run it.

'DelFolder.vbs - December 1, 2005
'Ramesh Srinivasan

Dim i, fso, f, f1, sf, BasePath, CalcResult, fNameArray()
BasePath = "D:\Reports"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(BasePath)
Set sf = f.SubFolders
For Each f1 in sf
     CalcResult = DateDiff("d",f1.DateCreated,Now)
          if CalcResult > 2 then
                ReDim preserve fNameArray(i)
                fNameArray(i) = f1.Name
                i = i + 1
        end if
Next

For Each fName in fNameArray
    FSO.DeleteFolder(BasePath & "\" & fName)
Next

You may also use Task Scheduler and configure this script to run daily.

Recent Posts