John Topley's Knowledgebase

Delete Temporary Files On A Drive

Friday, 30 May 2003

This Windows Script Host script illustrates walking a folder tree to delete all *.tmp files.

Download DeleteTempFiles.vbs

' Script to delete all *.TMP files within a given root folder.
Option Explicit
Private fsoFileSystem
Private fsoFile
Private strRootFolder

strRootFolder = InputBox("Enter root folder:", "Temp File
  Cleaner", "C:\")

If Len(strRootFolder) > 0 Then
    If Right(strRootFolder, 1) <> "\" Then
        strRootFolder = strRootFolder & "\"
    End If

    Set fsoFileSystem = CreateObject
      ("Scripting.FileSystemObject")

    WalkSubfolders fsoFileSystem.GetFolder(strRootFolder)
    WScript.Echo "Temporary files cleaned."
End If

Private Sub WalkSubFolders(fsoFolder)
    On Error Resume Next

    Dim fsoSubFolder

    For Each fsoSubFolder in fsoFolder.SubFolders
        For Each fsoFile In fsoSubFolder.Files
            If fsoFile.Type = "TMP File" Then                  
                fsoFile.Delete               
            End If
        Next

        WalkSubFolders fsoSubFolder
    Next
End Sub

Set fsoFileSystem = Nothing

top | index | previous | no next | comments ()

home | archive | kb | media | about | contact | accessibility
Copyright © 2003 - 2005 John Topley. Made with CityDesk.