
deleting logfiles older than current date
Everything you need is explained in
FileSystemObject Tutorial
http://msdn.microsoft.com/scripting/VBScript/doc/vbsfsotutor.htm
The example below uses error trapping around access to the DateCreated
property just to be safe. It is possible (but not common) for a file to not
have a vaild DateCreated. The example also has the actual delete commented
out and displays a list of files that would have been deleted instead. It's
always a good idea when automating a process to delete files to thoroughly
"sanity check" before enabling the delete logic. Files deleted with FSO
methods DO NOT go to the recycle bin.
=======================================
path = "<PathToLogfiles>"
set fso = CreateObject("Scripting.FileSystemObject")
oldestDateToKeep = dateadd("yyyy", -1, date()) 'a quarter ago...
oldestDateToKeep = dateadd("q", -1, date()) 'a quarter ago...
oldestDateToKeep = dateadd("m", -1, date()) 'a month ago...
oldestDateToKeep = dateadd("d", -7, date()) 'a week ago...
oldestDateToKeep = dateadd("d", -1, date()) 'yesterday...
oldestDateToKeep = date() 'today...
set folder = fso.getfolder(path)
s = "+++++oldestDateToKeep+++++" & vbcrlf
s = s & oldestDateToKeep & vbcrlf
s = s & "+++++selected+++++" & vbcrlf
s2= "+++++excluded+++++" & vbcrlf
for each file in folder.files
dtcreated = null
on error resume next
dtcreated = file.datecreated
on error goto 0
if isnull(dtcreated) then
'no valid create date...
elseif dtcreated < oldestDateToKeep then
s = s & file.name & vbcrlf
'file.delete true
else
'not old enough...
s2 = s2 & file.name & vbcrlf
end if
next
msgbox s & s2
--
Michael Harris
Microsoft.MVP.Scripting
--
Please do not email questions - post them to the newsgroup instead.
--
Quote:
> I have a directory where all logfiles are stored, how can i write a
> script that will delete all old logfiles.