Clear Recent

The following script will clear out “recent” documents, such as temporary files, temporary and cached files of Visual Studio, and so on.

ftp://missico.net/ClearRecent.vbs

Clear Recent.vbs

'HKCU\Software\Microsoft\Internet Explorer\Explorer Bars\{C4EE31F3-4768-11D2-BE5C-00A0C9A83DA1}\FilesNamedMRU
'HKCU\Software\Microsoft\Internet Explorer\Explorer Bars\{C4EE31F3-4768-11D2-BE5C-00A0C9A83DA1}\ContainingTextMRU
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\StreamMRU
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Map Network Drive MRU
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU
'HKCU\Software\Microsoft\Internet Explorer\TypedURLs
'HKCU\Software\Microsoft\Internet Explorer\IntelliForms\SPW
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist\{5E6AB780-7743-11CF-A12B-00AA004AE837}\Count
'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist\{75048700-EF1F-11D0-9888-006097DEACF9}\Count
'
'%USERPROFILE%\Cookies
'%USERPROFILE%\History
'%USERPROFILE%\Recent
'%USERPROFILE%\Local Settings\Temporary Internet Files
'%USERPROFILE%\Local Settings\History
'%USERPROFILE%\UserData
'%USERPROFILE%\Application Data\Microsoft\Office\Recent
'%USERPROFILE%\Application Data\Microsoft\VisualStudio\7.1\ProjectAssemblies
'%USERPROFILE%\Local Settings\Apps
'%USERPROFILE%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data
'%SystemDrive%\recycled
'%SystemDrive%\RECYCLER
'%windir%\temp
'%temp%

'%windir%\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
'%windir%\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files

Option Explicit

Const HKEY_CLASSES_ROOT  = &H80000000
Const HKEY_CURRENT_USER  = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS         = &H80000003

Dim wmiLocator
Dim wshNetwork 
Dim wmiNameSpace
Dim objRegistry

Dim oFSO    'As FileSystemObject
Dim oShell  'As WScript.Shell


Main

Public Sub Main()

    InitializeObjects
    
    ClearRecent
    ClearVS
    ClearVS7    
    ClearVS8
    ClearVS9
    
    ClearMDM

    ClearTemporary
    ClearWindowsTemporary
    
    ClearStupidFlash

    'ClearExplorerStreams
    ClearExplorerCommonDialog
    'ClearExplorerMenuOrder
    ClearCustomNotifications

    WScript.Echo "Done!"
    
End Sub

Private Sub InitializeObjects

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oShell = WScript.CreateObject("WScript.Shell")

    ' Object used to get StdRegProv Namespace
    Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")

    ' Object used to determine local machine name
    Set wshNetwork = CreateObject("WScript.Network")

    ' Registry Provider (StdRegProv) lives in root\default namespace.
    Set wmiNameSpace = wmiLocator.ConnectServer(wshNetwork.ComputerName, "root\default")
    Set objRegistry = wmiNameSpace.Get("StdRegProv")

End Sub

Private Sub ClearStupidFlash
   ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Macromedia")
End Sub

Private Sub ClearRecent
   ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Office\Recent")
   ClearFolder(oShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Recent")
End Sub

'C:\Documents and Settings\amissico\Local Settings\Application Data\Microsoft\WebsiteCache

Private Sub ClearVS
    ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\VisualStudio\7.1\ProjectAssemblies")
    ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\VisualStudio\Recent")
    ClearFolder(oShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Local Settings\Application Data\Microsoft\WebsiteCache")
End Sub

Private Sub ClearVS7
    ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\VisualStudio\7.1\ProjectAssemblies")
    ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\VisualStudio\Recent")
    ClearFolder(oShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Local Settings\Application Data\ApplicationHistory")
    ClearFolder(oShell.ExpandEnvironmentStrings("%WINDIR%") & "\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files")
End Sub

Private Sub ClearVS8
    ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\VisualStudio\8.0\ProjectAssemblies")
    ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\VisualStudio\8.0\Recent")
    ClearFolder(oShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Local Settings\Application Data\ApplicationHistory")
    ClearFolder(oShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies")
    ClearFolder(oShell.ExpandEnvironmentStrings("%WINDIR%") & "\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files")
    
End Sub

Private Sub ClearVS9
    ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\VisualStudio\9.0\ProjectAssemblies")
    ClearFolder(oShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\VisualStudio\9.0\Recent")
    ClearFolder(oShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Local Settings\Application Data\ApplicationHistory")
    ClearFolder(oShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Local Settings\Application Data\Microsoft\VisualStudio\9.0\ProjectAssemblies")
    ClearFolder(oShell.ExpandEnvironmentStrings("%WINDIR%") & "\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files")
End Sub

Private Sub ClearFolder(sFolder)
    Dim oFldr   'As Folder
    Dim oFile   'As File
    
    On Error Resume Next

    Set oFldr = oFSO.GetFolder(sFolder)

    If Err.number = 0 Then
    
        oFldr.Delete True
        
        If Err.Number = 70 Then 'permission denied, usually a locked file
            Err.Clear
            'continue
            
            'try to delete individual files
            
            For Each oFile In oFldr.Files
                oFile.Delete True
                If Err.Number = 0 Then
                    'deleted okay
                ElseIf Err.Number = 70 Then
                    'ignore
                    Err.Clear
                Else
                    MsgBox Err.Number & " - " & Err.Description
                End If
            Next
            
        ElseIf Err.Number = 0 Then
            'deleted            
        Else
            MsgBox Err.Number & " - " & Err.Description
        End If
   
    ElseIf Err.Number = 76 Then
        'ignore, path not found
    Else
        MsgBox Err.Number & " - " & Err.Description
    End If
   
End Sub

Private Sub ClearMDM
'Machine Debug Manager

   Dim oFldr   'As Folder
   Dim oFile   'As File
   
   Set oFldr = oFSO.GetSpecialFolder(0) 'WindowsFolder
   For Each oFile In oFldr.Files
      If UCase(Left(oFile.Name, 3)) = "FFF" Then
         oFile.Delete True
      End If
   Next
End Sub

Private Sub ClearWindowsTemporary()
   Dim oFldr   'As Folder
   Dim oFldrs  'As Folders
   Dim oFile   'As File
   
   Set oFldr = oFSO.GetFolder(oShell.ExpandEnvironmentStrings("%WINDIR%") & "\Temp")

   On Error Resume Next
   
   'files may be locked by an existing process
   '70 (0x800A0046)

   For Each oFile In oFldr.Files
      oFile.Delete True
      If Err.Number = 70 Then
         Err.Clear
      End If
   Next

   On Error GoTo 0
   Set oFldrs = oFldr.SubFolders
   For Each oFldr In oFldrs
      oFldr.Delete True 'and contents are deleted
   Next
   
End Sub

Private Sub ClearTemporary()
   Dim oFldr   'As Folder
   Dim oFldrs  'As Folders
   Dim oFile   'As File
   
   Set oFldr = oFSO.GetSpecialFolder(2) 'TemporaryFolder

   On Error Resume Next
   
   'files may be locked by an existing process
   '70 (0x800A0046)

   For Each oFile In oFldr.Files
      oFile.Delete True
      If Err.Number = 70 Then
         Err.Clear
      End If
   Next

   On Error GoTo 0
   Set oFldrs = oFldr.SubFolders
   For Each oFldr In oFldrs
      oFldr.Delete True 'and contents are deleted
   Next
   
End Sub

Private Sub ClearCustomNotifications()
    ClearRegistryKey HKEY_CURRENT_USER, "HKCU", "Software\Microsoft\Windows\CurrentVersion\Explorer\TrayNotify"
End Sub

Private Sub ClearExplorerStreams()
    ClearRegistryKey HKEY_CURRENT_USER, "HKCU", "Software\Microsoft\Windows\CurrentVersion\Explorer\StreamMRU"
    ClearRegistryKey HKEY_CURRENT_USER, "HKCU", "Software\Microsoft\Windows\CurrentVersion\Explorer\Streams"
End Sub

Private Sub ClearRegistryKey(lngHive, sHive, sKey)
'only keys, not values

    On Error Resume Next

    oShell.RegDelete sHive & "\" & sKey & "\"
    If Err.Number = 0 Then
        'okay
    ElseIf Err.Number = -2147024894 Then
        'unable to remove registry key <name>
        'cause is key doesn't exist
    ElseIf Err.Number = -2147024891 Then
        'unable to remove registry key <name>
        'cause is key has subkeys
        DeleteRegEntry lngHive, sKey
        
    Else
        MsgBox err.number & " - " & err.description
    End If
End Sub

Private Sub ClearExplorerCommonDialog()
    ClearRegistryKey HKEY_CURRENT_USER, "HKCU", "Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32"
End Sub

Private Sub ClearExplorerMenuOrder()
    ClearRegistryKey HKEY_CURRENT_USER, "HKCU", "Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder"
End Sub

Public Function DeleteRegEntry(hndHive, sEnumPath)
    Dim sKeyName    'As String
    Dim sNames      'As String()
    Dim lRC         'As Long

    'attempt to delete, if fails, start subkey enumration process.
    lRC = objRegistry.DeleteKey(hndHive, sEnumPath)

    If (lRC <> 0) Then
        'deletion failed, start deleting subkeys.

        On Error Resume Next

        lRC = objRegistry.EnumKey(hndHive, sEnumPath, sNames)

        For Each sKeyName In sNames
            If Err.Number <> 0 Then Exit For
            lRC = DeleteRegEntry(hndHive, sEnumPath & "\" & sKeyName)
        Next

        On Error Goto 0

        'when we get here, we should have looped through all subkeys
        'try to delete the registry key again
        lRC = objRegistry.DeleteKey(hndHive, sEnumPath)

    End If

End Function
    

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment