Delete Old Folders

June 17, 2004

>>Script Language and Platform: MSSQL/VBScript
This VBScript can be used to delete old folders.

'Usage: DeleteOldFolder "\\Servername1\Sharename\Replication\" "3" "\\servername2\log\DeleteOldFolders.log"

'Usage: DeleteOldFolder "d:\MSSQL\BACKUP\Replication\" "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 "\\Servername\d$\MSSQL\BACKUP\Replication\" "2" "C:\mylog.txt"
'Usage: DeleteOldFolder "\\Servername1\Sharename\Replication\" "3" 
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" & "'\\Servername1\Sharename\Replication\' '3' 
end if
Dim Directory 
Dim Noofdays
Dim FSO2
Dim LogFile
Dim Folderlist 
Dim folders 
Directory =WScript.Arguments(0)
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 
ofile.writeline cstr(now()) & "    " & tempdirectory & " has been deleted"
end if
'       msgbox tempdirectory & " has been deleted"
       End If
'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

The Network for Technology Professionals


About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers