How to Delete Old Folders usign a VisualBasic Script (VBScript)

Send Us a Sign! (Contact Us!)
Word PDF Epub Text
XML OpenOffice XPS MHT

This VBScript can be used to delete old folders.

Usage (UNC Path):

DeleteOldFolder "\\Servername1\Sharename\Replication\" "3" "\\servername2\log\DeleteOldFolders.log"

[tweet]

Usage (Local Path):

DeleteOldFolder "d:\MSSQL\BACKUP\Replication\" "2" "C:\mylog.txt"
'Objective: To Delete folders older than certain # of days.

'Usage: DeleteOldFolder "\\Servername\d$\MSSQL\BACKUP\Replication\" "2" "C:\mylog.txt"
'Usage: DeleteOldFolder "\\Servername1\Sharename\Replication\" "3" "\\servername2\log\DeleteOldFolders.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" & "'\\Servername1\Sharename\Replication\' '3' '\\servername2\log\DeleteOldFolders.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()

SOURCE

LINK

LANGUAGE
ENGLISH

1 thought on “How to Delete Old Folders usign a VisualBasic Script (VBScript)”

Comments are closed.