Delete Old Folders

>>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

Latest Articles