Colin’s IT, Security and Working Life blog

February 18, 2010

VB Script to delete files older than a few days

Filed under: Uncategorized — chaplic @ 5:35 pm

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
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
  Set fso = CreateObject("Scripting.FileSystemObject")
  on error resume next

  Set folder = fso.GetFolder(strDir)
  if err = 0 then
    strComments = strComments & strDir & " exists." & VBCRLF
    strComments = strComments & ". **ERROR** Accessing folder – cannot find " & strDir & VBCRLF &  err.description & " (" & err.source & ")" & VBCRLF

  end if

  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
                    strComments = strComments & ". SUCCESS"
                    strComments = strComments & ". **ERROR** " & err.description & " (" & err.source & ")"
                  end if
       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
  strComments = strComments & VBCRLF & "No of Files Deleted: " & intDeleted & VBCRLF & "ERROR in deleting: " & intNotDeleted

  if intNotDeleted > 0 then
    logit strComments, ERROR
    logit strComments,INFORMATION
  end if
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

end Function


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: