Delete Old Folders | Database Journal

Delete Old Folders

Jun 17, 2004
1 minute read

>>Script Language and Platform: MSSQL/VBScript

This VBScript can be used to delete old folders.

‘Usage: DeleteOldFolder “\Servername1SharenameReplication” “3” “\servername2logDeleteOldFolders.log”

‘Usage: DeleteOldFolder “d:MSSQLBACKUPReplication” “2” “C:mylog.txt”

Author: MAK


‘Objective: To Delete folders older than certain # of days.
‘Created by: MAK
‘Date Written: Jun 4, 2004
‘Usage: DeleteOldFolder “\Servernamed$MSSQLBACKUPReplication” “2” “C:mylog.txt”
‘Usage: DeleteOldFolder “\Servername1SharenameReplication” “3”
	“\servername2logDeleteOldFolders.log”
On error resume next
if isnull(WScript.Arguments(0))=True or isnull(WScript.Arguments(1))=True
	or  isnull(WScript.Arguments(2))=True then
msgbox “Usage: DeleteOldFolder” & “‘\Servername1SharenameReplication’ ‘3’
	‘\servername2logDeleteOldFolders.log'”
end if
Dim Directory
Dim Noofdays
Dim FSO
Dim FSO2
Dim LogFile
Dim Folderlist
Dim folders 
Directory =WScript.Arguments(0)
Noofdays=cint(WScript.Arguments(1))
LogFile=WScript.Arguments(2)
Set FSO = CreateObject(“Scripting.FileSystemObject”)
Set FSO2 = CreateObject(“Scripting.FileSystemObject”)
Set oFSO = CreateObject(“Scripting.FilesyStemObject”)
Set ofile = oFSO.createTextFile(LogFile, True)
ofile.writeline “DeleteOldFolders Started”
ofile.writeline now()
Set Folderlist = FSO.GetFolder(Directory)
Set folders = Folderlist.SubFolders
        For Each d In Folders
‘          msgbox d.name
‘          msgbox d.size
‘          msgbox d.dateCreated
‘          msgbox d.dateLastModified
‘          msgbox d.dateLastAccessed
tempdirectory = Directory & d.name 
‘msgbox Noofdays
‘msgbox datediff(“d”,d.dateCreated,now())
If  datediff(“d”,d.dateCreated,now()) > Noofdays Then
       FSO2.DeleteFolder(tempdirectory )
ofile.writeline cstr(now()) & ”    ” & tempdirectory & ” Delete Started”
if err.number <>0 then
ofile.writeline cstr(now()) & ”    ” & Err.description
err.clear
else
ofile.writeline cstr(now()) & ”    ” & tempdirectory & ” has been deleted”
err.clear
end if
‘       msgbox tempdirectory & ” has been deleted”
       End If
Next
‘msgbox “Done”
ofile.writeline “DeleteOldFolders Completed”
ofile.writeline now()



Disclaimer:
We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.