Yes, it’s the year 2015, and I still find myself creating VBScripts from time to time. My client has an old server that uses a third party tool to do database extracts, and it supports only VBScript. So we have scripts that email CSV (Comma Separated Value) extracts, and/or copy them to various folders on different servers (or to FTP sites).

I wanted to make sure a script cleaned-up old files that were created, including Trace files. Normally you want a retention period of x days. So I created a function that takes three parms:

  1. Days to delete (if older than)
  2. Folder Path
  3. File Mask (only delete files containing this string)
' Neal Walters  09/17/2015 

countFilesDeleted = DeleteFiles(7,"E:\MESSAGES\Folder1","PlanITROI_Hourly_Order_Status_") 
WScript.Echo("Count of Files Deleted = " & countFilesDeleted) 
countFilesDelested = DeleteFiles(21,"E:\MESSAGES\Folder2\VBScriptsTrace","Trace") 
WScript.Echo("Count of Files Deleted = " & countFilesDeleted) 

Function DeleteFiles(Days,FolderName,FileMask) 
   set fso2 = CreateObject("Scripting.FileSystemObject")
   set f = fso2.GetFolder(FolderName) 
   set fc = f.Files 
   countDeletes = 0 
   for each f1 in fc 
      if DateDiff("d", f1.DateCreated, Date) > Days Then
	     if instr(f1.Name,FileMask) > 0 then 
			WScript.Echo "Deleting File: " & f1.Name 
			countDeletes = countDeletes + 1 
		 end if 
      end if
    Set fso2 = Nothing 	
	DeleteFiles = countDeletes 
End Function 


Compare to Powershell cleanup/delete files script.

Filed under: VBScript