You are here:

Excel/Delete excel temp files code


Hi Bob,
Excel 2003 Windows XP

I found this code on the internet. Would you take a look and advise whether it deletes temp excel files. I plan to run it upon opening the workbook. Is there a simpler way to do this (code)

Thank you

' File: tempDirTidy
' Function: deletes inactive files from the Windows Temp directory
' i.e. files not automatically deleted by the creating process

Const TemporaryFolder = 2 'for GetSpecialFolder

Set FSO = CreateObject("Scripting.FileSystemObject")

'init an empty array (UBound will be -1)...
'we use an array and store the file objects as this avoids any problems
' with altering the contents of the Files collections while they
' are being iterated.

arFiles = Array()
count = -1

'get the path to the temp folder
tempdir = FSO.GetSpecialFolder(TemporaryFolder)

'load the (global scope) array arFiles
'SelectFiles calls itself recursively for SubFolders

SelectFiles tempdir

'the error trap is in case any are in-use...

dCount = 0
For Each file In Arfiles
On Error Resume Next
file.Delete True
If Err.Number = 0 Then
dCount = dCount + 1
End If
On Error Goto 0

'now go back and delete empty folders below the temp folder

DeleteEmptyFolders tempdir,False

'comment out for "silent" operation,
' or add support for a "/s" command-line switch.

Msgbox count+1 & " files were found in " & tempdir & vbcrlf & dCount & " of
those files were deleted.", vbInformation,"Windows Temp Directory Tidy-up"

Sub SelectFiles(sPath)

'select files to delete and add to array...

Set Folder = FSO.Getfolder(sPath)
Set Files = Folder.Files

For Each file In Files
Count = Count + 1
Redim Preserve arFiles(Count)
Set arFiles(Count) = file

For Each fldr In Folder.Subfolders
Selectfiles Fldr.Path

End Sub

Sub DeleteEmptyFolders(sPath,fDeleteThisFolder)

Set Folder = FSO.GetFolder(sPath)

'recurse first...

For Each fldr In Folder.Subfolders
DeleteEmptyFolders fldr.Path,True

'if no files or folders then delete...

'fDeleteThisFolder is False for the root of the subtree,
' and true for sub-folders (unless you want to delete
' the entire subtree if it is empty).

If (Folder.Files.Count = 0) And _
(Folder.Subfolders.Count) = 0 And _
fDeleteThisFolder Then
Exit Sub
End If

End Sub

Best way to tell is to try it and see if it deletes temp files.
This is too long for me to test, and since I don't have my own version, I can't advise if there's a better way.
Sorry I can'y be of more help here.
It's also pretty easy to delete Temp files manually.
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Bob Umlas


I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at


Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Excellence, The Expert, Microsoft

BA in math, Hofstra University, 1965

Awards and Honors
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 All rights reserved.