Created
June 13, 2011 10:41
-
-
Save sabate/1022585 to your computer and use it in GitHub Desktop.
Delete Old Log Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' DelOldLogs.vbs | |
' *************************************************** | |
' Script to delete files in a folder and sub-folders when files are x number of days old. | |
Path = "C:\jboss-1\server\default\log" | |
' * Alter this variable, if sub-folders should be processed | |
Subfolders = False | |
' * Alter this variable to set the how many days old the file should be before it is deleted. | |
' * Example : lifetime = date() - 10 . Will delete files that are 10 days old or more | |
Lifetime = date() - 9 | |
FilesArray = Array() | |
set fso = createobject("scripting.filesystemobject") | |
SelectFiles path, lifetime, FilesArray, Subfolders | |
numDeleted = 0 | |
for n = 0 to ubound(FilesArray) | |
on error resume next | |
FilesArray(n).delete true | |
on error goto 0 | |
next | |
sub SelectFiles(sPath,vlifetime,FilesArrayToKill,bIncludeSubFolders) | |
on error resume next | |
set folder = fso.getfolder(sPath) | |
set files = folder.files | |
for each file in files | |
dtlastmodified = null | |
on error resume Next | |
dtlastmodified = file.datelastmodified | |
on error goto 0 | |
if not isnull(dtlastmodified) Then | |
if dtlastmodified < vlifetime then | |
count = ubound(FilesArrayToKill) + 1 | |
redim preserve FilesArrayToKill(count) | |
set FilesArrayToKill(count) = file | |
end if | |
end if | |
next | |
' If sub-folders are selected, call the procedure again to update the array with the contents. | |
if bIncludeSubFolders then | |
for each fldr in folder.subfolders | |
SelectFiles fldr.path,vlifetime,FilesArrayToKill,true | |
next | |
end if | |
end sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment