IT Management Daily
Storage Daily
Security Daily
FREE NEWSLETTERS
search
 

follow us on Twitter


internet.commerce
Be a Commerce Partner















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers




Products
 DBX to CSV Extractor (DevSlide, Inc.)
 Skype Records (ABTO LLC)
 Orion Network Performance Monitor (SolarWinds)
 FB Limiter (AxiomCoders)
 Keylogger Free Download (Free keylogger download)
 Doremisoft Mac PDF to HTML Converter (Doremisoft Studio)
» Enterprise IT Planet » Resources » Win Scripts

Delete Temp Files and Folders

February 20, 2009

Email Print Digg This Add to del.icio.us

Want to share a script? Click here to contribute!

Author:
Shane Boudreaux

Platform:
Windows

Type:
Win

Description:
Deletes files and folders recursively from a defined root folder, for example c:temp.

Scroll down to view the script.


Delete Temp Files and Folders


On 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.

Email Print Digg This Add to del.icio.us

Win Scripts Archives




Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers