VbScript Delete files older an specific date


Scenario:

I want to delete all the SQL backups that have more than a week.

Dim NumberOfDays, FileLocation

NumberOfDays = 7

FileLocation =  “D:\BackupFiles”

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(FileLocation)
Set colSubfolders = objFolder.Subfolders
Set colFiles = objFolder.Files

For Each objSubfolder in colSubfolders

Set colFiles = objSubfolder.Files
For Each objFile in colFiles

If objFile.DateLastModified < (Date() – NumberOfDays) Then
objFile.Delete
End If

Next

Next

Don’t forget to save this in a .vbs file

hope that this information can be useful

Advertisements

About rodvars
Been working in IT Services/Consulting for the past 15 years. My main areas of work are planning, development, managing and administration System infrastructures focusing on optimizing user processes, enforcing business security, performance enhancements, high availabilty and infrastucture scalability.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: