' ' Script to delete files older than a given number of days. ' ' WARNING: Files deleted with this program could not be recovered. They don't even get to Recycle Bin. ' Use at your own risk. ' ' (C) Copyright, 2002. Ferran Foz. Barcelona, Spain. ferran@ostres.com ' ' You may copy and distribute without changing the copyright notice. ' ' Set StdOut = WScript.StdOut Main Sub Main() ' ' DropOldFiles ' folderspec - Folder where you want to delete files ' logfile - File where logfile will be generated. ' days - Number of days to keep files. ' Call DropOldFiles("C:\zzTEMP\","C:\TEMP\LOG.TXT", 100) End Sub Sub DropOldFiles(folderspec, logfile, days) Dim fso,f,f1,s,fi, cnt, tot StdOut.WriteLine "Starting DropOldFiles..." LogOpen(logfile) cnt = 0 tot = 0 Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder(folderspec) Set fc = f.Files For Each f1 in fc If DateDiff ("d",f1.DateLastModified, Now ) > days Then LogWrite f1.name & " DateLastModified:" & f1.DateLastModified & " DaysOld:" & DateDiff ("d",f1.DateLastModified, Now ) fso.DeleteFile (folderspec & f1.name) cnt = cnt + 1 End If tot = tot + 1 Next LogWrite "--------------------------------------------------------------" LogWrite " Processed " & tot & " file(s). Deleted " & cnt & " file(s)." LogWrite "--------------------------------------------------------------" LogClose StdOut.WriteLine "Ending DropOldFiles..." End Sub ' ' ***************************************************************************************** ' ' ' Write to the Logfile ' Dim LogFile Sub LogWrite (Text) LogFile.WriteLine "[" & Now & "] " & Text End Sub Sub LogClose () LogFile.Close Set Logfile=nothing End Sub Sub LogOpen (LogPath) Dim fso, f1 Const ForAppend = 8 Set fso = CreateObject("Scripting.FileSystemObject") Set LogFile = fso.OpenTextFile(LogPath, ForAppend, True) Set fso = nothing End Sub