deleting logfiles older than current date 
Author Message
 deleting logfiles older than current date

I have a directory where all logfiles are stored, how can i write a
script that will delete all old logfiles.


Mon, 09 Jun 2003 04:20:40 GMT  
 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.



Mon, 09 Jun 2003 09:45:54 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Deleting log files older than current date

2. Script to delete files older than a specific date in a directory

3. delete records that are older than a certain date

4. Script to delete files older than a specific date in a directory

5. Deleting files and folders older than a particular date

6. Displaying the Current Date and Current Time

7. Deleting old files

8. Delete Files 3 days old

9. Delete old records ?

10. Find files in a folder and delete them if older then 2 dayx

11. Delete files older than n days.

12. Delete oldest file

 

 
Powered by phpBB® Forum Software