|
|
Delete Temp Files and FoldersFebruary 20, 2009
Want to share a script? Click here to contribute! Author:
Platform: Type:
Description: Scroll down to view the script. Delete Temp Files and FoldersOn Error Resume Next ''=============================================== '' GLOBAL CONSTANTS & DECLARES ''=============================================== Const LOG_FILE = "C:_StuffDelLogdelLog.txt" Const LOG_FORMAT = "================================================" Dim intDepth, logText, errText, arTempFolders ''=============================================== ''=============================================== '' MAIN ENTRY POINT ''=============================================== '' construct array with all root temp folders arTempFolders = Array("C:Documents and SettingstomLocal SettingsTemporary Internet Files", "C:Documents and SettingstomLocal SettingsHistory", "C:Documents and SettingstomCookies", _ "C:Documents and SettingstomLocal SettingsTemp", "C:Temp", "C:WINXPTemp", "C:Documents and SettingstomLocal SettingsApplication DataMicrosoftMedia Player") '' iterate thru array, deleting from each root temp folder For i = LBound(arTempFolders) To UBound(arTempFolders) Set objShell = CreateObject("Shell.Application") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objNameSpace = objShell.Namespace(arTempFolders(i)) Set objFolderItem = objNameSpace.Self set objFolder=objFSO.GetFolder(objFolderItem.Path) intDepth = 0 '' delete files & sub-folders removeFolder objFolder Next '' log results logToFile LOG_FILE, LOG_FORMAT & vbcrlf & Now & vbcrlf & errText & LOG_FORMAT & vbcrlf & logText & LOG_FORMAT ''=============================================== '' EXIT POINT ''=============================================== ''=============================================== '' SUPPORTING FUNCTIONS & PROCEDURES ''=============================================== Private Sub removeFolder(objFolder) '' build log text logText = logText & objFolder & vbcrlf '' Recursively remove files and folders intDepth = intDepth + 1 On Error Resume Next For each objFile in objFolder.Files objFile.Delete True If Err.Number > 0 Then errText = errText & Err.Description & " == " & objFile & vbcrlf Err.Clear End If Next Err.Clear On Error GoTo 0 For each objSubfolder in objFolder.SubFolders removeFolder objSubFolder Next intDepth = intDepth - 1 If intDepth <> 0 Then '' Don''t delete top-level folder On Error Resume Next objFolder.Delete True Err.Clear On Error GoTo 0 End If End Sub ''=============================================== Private Sub logToFile(logFile, txt) Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile(logFile, True) objFile.WriteLine(txt) objFile.Close Set objFSO = Nothing End Sub ''=============================================== Disclaimer: We hope that the information in these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.
|
|