I had a client that for a variety of reasons moved the Outlook Temporary files to a different folder from the default. It was noticed that outlook wouldn’t always delete it’s tempoary files, so someone had hacked together a little VB script to do the job.
However, it appeared to be buggy and didn’t delete all files. No obvious reason why. Also, if the temp area wasn’t available when it was run, it would error to the screen.
To help, I concocted this script. I’m no VBScript guru, but with looking at examples on the web pulled this together. It majors on letting the user know what it’s doing and trapping errors. The program style isn’t perfect, but it works.
Most importantly of all, we found the bugs in the previous effort; some outlook attachments were marked as read only and therefore not deleted; this script gets round that by forcing deletion – only stopped by permissions problems.
Here it is in all its glory..
Option Explicit
‘ Path to delete
const FolderName ="d:\Temp"
‘ Age of file (last modified) in days before it is deleted
const Days=7
‘Set debug to 1 for screen pops with helpful messages
const debugmode=0
const SUCCESS=0
const ERROR=1
const WARNING=2
const INFORMATION=4
dim result
logit "Begining process to delete temp files in " & FolderName & " older than " & Days & " Days",INFORMATION
result = delFiles(FolderName,Days)
Function delFiles(strDir,strDays)
‘ Take in the path to delete and the number of days old a file has to be
‘ Then delete the files in that path if they are older than that date
dim fso,file,folder,individualFile,foldercontains
dim strComments
dim intDeleted, intNotDeleted
intDeleted=0
intNotDeleted=0
Set fso = CreateObject("Scripting.FileSystemObject")
on error resume next
Set folder = fso.GetFolder(strDir)
if err = 0 then
strComments = strComments & strDir & " exists." & VBCRLF
else
strComments = strComments & ". **ERROR** Accessing folder – cannot find " & strDir & VBCRLF & err.description & " (" & err.source & ")" & VBCRLF
intNotDeleted=intNotDeleted+1
end if
err.Clear
Set foldercontains = folder.Files
dim intDifferenceinDays
strComments = strComments & "Deleting Files older than " & strDays & " days" & VBCRLF
For Each individualFile in folderContains
‘ Loop through each file in the folder and check its date
if debugmode=1 then
wscript.echo ("Looking at " & individualfile & VBCRLF & "Which has date last modified of: " & individualFile.datelastmodified _
& VBCRLF & "To see if its " & strDays & " days older than " & Now)
End if
strComments = strComments & VBCRLF & "Analysed " & individualfile & ": "
intDifferenceinDays = DateDiff("d", individualFile.datelastmodified, Now)
If intDifferenceinDays > strDays Then
if debugmode=1 then
wscript.echo ("We’ve decided to delete" & file &"… Datediff is" & intDifferenceinDays )
End if
strComments = strComments & " Deleting…."
on error resume next
fso.DeleteFile individualFile,TRUE
if err = 0 then
intDeleted=intDeleted+1
strComments = strComments & ". SUCCESS"
else
intNotDeleted=intNotDeleted+1
strComments = strComments & ". **ERROR** " & err.description & " (" & err.source & ")"
end if
err.Clear
Else
if debugmode=1 then
wscript.echo ("We’ve decided to spare" & file &"… DiD: " & intDifferenceinDays & " Required: " & strDays)
End if
strComments = strComments & "Not Deleted. Only " & intDifferenceinDays & " days old"
End If
Next
strComments = strComments & VBCRLF & "No of Files Deleted: " & intDeleted & VBCRLF & "ERROR in deleting: " & intNotDeleted
if intNotDeleted > 0 then
logit strComments, ERROR
else
logit strComments,INFORMATION
end if
delFiles=1
End Function
Function logit(text,level)
‘ Writes a simple message to the windows event log
dim Wshshell
set WshShell = CreateObject("WScript.Shell")
WshShell.LogEvent level, text
logit=1
end Function