Delete Cache VBS VBScript to Delete all Files in a Folder
April 10th, 2009 | Published in Programming, technology
This is a simple VBS VBScript that I use to delete all files in a given folder on a machine. It is particularly useful to delete internet cache files or cookies. You can set a scheduled job actually, to perform this task nightly. This script could be easily modified to, say, clear out your temporary internet files from the folder used by internet explorer. This example is a vbs vbscript to delete files in a temporary folder called “downloads” contained within my “My Documents” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | ' VB Script Document, VBS, WSH, wscript ' Deletes files in a specified folder, for example, a temp downloads folder. ' By GR, February 27, 2009. ' robarspages.ca 'Declare variables Dim strCacheFile Dim strComputer Dim strAppData Dim strUserProfile Dim strErrorfile Dim objWMIService Dim objShell Dim objFSO Dim objFolder StrcacheFile = "\downloads\" strComputer = "." 'leave alone if running script on same maching Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objShell = CreateObject( "WScript.Shell" ) '---path to the application data folder. not used in this example. strAppData = objShell.ExpandEnvironmentStrings("%APPDATA%") '---path to the user folder (if multiple users) strUserProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%") Set objFSO = CreateObject("Scripting.FileSystemObject") ' get the folder that contains the folder you want to empty. Set objFolder = objFSO.GetFolder(strUserProfile & "\My Documents") If objFSO.FolderExists(objFolder & StrcacheFile) Then objFSO.DeleteFile(objFolder & StrcacheFile & "*"), True WScript.Echo "Folder """ & objFolder & StrcacheFile & """ was emptied successfully" else WScript.Echo "ERROR: I could not find the folder, please check the path..." End If ' Clean up objects Set objWMIService = Nothing Set objShell = Nothing Set objFSO = Nothing Set objFolder = Nothing |