Sunday, January 7, 2007

How to automatically delete the folders that are more than 'n' days old?

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 - jan 7 2007
Devesh Mankar

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

1 comment:

Devesh said...

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


source: http://windowsxp.mvps.org/wshdelfolder.htm

 
.CO.NR Free Domain