Monday, March 23, 2009

Delete backup files n days older

Delete backup files n days older

Its the continuation of the last post, if you don't want to keep all the backup files, you want to store only "n" days back up and delete the older directories/ backup files. The vb script given below can do it.




' if you are going to run the script from a specific location, you can give as the value for Folderspec eg:- Folderspec="D:\BackUp"

Folderspec=""

Dim fs, f, f1, fc, s, sCurPath ,strDirectory, curDt,DelCount

'Current Date
CurDt=date()

DelCount=0

'Check any path is given with the call else finds the current directory
if len(folderspec)=0 then
'Find the current Directory
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
else
sCurPath=folderspec
end if

'Create file system object
Set fs = CreateObject("Scripting.FileSystemObject")

'selects the parent folder
Set f = fs.GetFolder(sCurPath)

'select the subfolders
Set fc = f.SubFolders

' looping through the directory contents

For Each f1 in fc

s = f1.name

' replace _ with / to convert to valid date

strDirectory = replace(s,"_","/")

'Check the date diffenrnce if it is greater than 3 (more than 3 days older) delete the folder

if DateDiff("d",strDirectory,CurDt) >3 then
DelCount=Delcount+1
fs.DeleteFolder(sCurPath & "\" & s)

end If

Next

wscript.echo "Total " & DelCount & " folders deleted from "& sCurPath


This article can be found at the blog section of my personal site www.dileepk.info

No comments: