' ' CleanUp text file from _Trial08.vbs ' ' Notes: Under Windows 7 use RunAs or Administrator to run script. ' ' JB 11:04 2011-05-25 ' ' Modifications: ' Added 'force' to Delete. 11:04 2011-05-25 JB ' NOTES: ' This script does NOT work under Vista or Win7. ' Security constraints prevent the access to the temporary folders. ' Either do it manually or use RunAs or an administrator (if you can find one!). ' Dim strMsg, intSize, colItems Dim strTemp, strtmp1, strTmp2 Dim fldr1, fldr2 '-- strMsg = "What does this 'program' do?" & vbCr & "It 'Cleans Up' your temporary folders." & vbCr & _ "It tells you where they are and how many files and bytes these files are taking up." & vbCr & _ "This script is a 'text file'. If you haven't already done so, please 'rename' it CleanUp.VBS." If (MsgBox(strMsg, 64+1, "This program is called 'CleanUp.VBS'.") = 1) Then ' ' WScript.Echo WScript.Version '------------- Constants --------------------------------- Const RECYCLE_BIN = &Ha& Const PROGRAM_FILES = &H26& Const MY_DOCUMENTS = &H5& Const NETHOOD = &H13& Const NETWORK_CONNECTIONS = &H31& Const MY_PICTURES = &H27& Const MY_COMPUTER = &H11& Const DESKTOP = &H10& Const ALL_USERS_DESKTOP = &H19& Const COOKIES = &H21& '-- Const FILE_SIZE = 3 '-------------------------- Program Start ------------------------- '-- intSize = 0 '-- Set FSO = CreateObject("Scripting.FileSystemObject") '-- Set objShell = CreateObject("Shell.Application") '-- Set objFolder = objShell.Namespace(RECYCLE_BIN) '-- Set colItems = objFolder.Items '-- For Each objItem in colItems strSize = objFolder.GetDetailsOf(objItem, FILE_SIZE) arrSize = Split(strSize, " ") intSize = intSize + CLng(arrSize(0)) Next '-- strMsg = "Recycle Bin has files totalling: " & vbCr '-- If intSize > 10240 Then strMsg = strMsg & "You should clean out the Recycle Bin as well. It is " End If '-- MsgBox strMsg & FormatNumber(intSize/1024, 0) & " KB in size.", 64, WScript.FullName '-- First check the 'System's temporary folder. Set WshShell = WScript.CreateObject("WScript.Shell") '-- System's TEMP folder Set WshSysEnv = WshShell.Environment("SYSTEM") '-- Expand it... strTmp1 = WshShell.ExpandEnvironmentStrings(WshSysEnv("TEMP")) '-- Set WshSysEnv = WshShell.Environment("PROCESS") '-- strTmp2 = WshShell.ExpandEnvironmentStrings(WshSysEnv("TEMP")) '-- Get object/folder. Set fldr1 = fso.GetFolder(strTmp1) ' Get object/folder. Set fldr2 = fso.GetFolder(strTmp2) '-- line continues below (after _ ) strTemp = strTmp1 & vbCR & "System TEMP, Number of Files: " & fldr2.Files.Count & " size: " & FormatNumber(fldr2.Size, 0) & " Bytes." & vbCr strTemp = strTemp & vbCR & strTmp2 & vbCr & "User TEMP, Number of Files: " & fldr1.Files.Count & " size: " & FormatNumber(fldr1.Size, 0) & " Bytes." & vbCR '-- MsgBox strTemp, 64, "Windows System's TEMP folder & User (PROCESS) TEMP folder" '-- Now we try and delete these temporary files... Set Files = Fldr1.Files '-- If 1 = Files.Count Then ' S = S & "There is 1 file" & NewLine Else ' S = S & "There are " & Files.Count & " files" & NewLine End If '-- Now we try and delete these temporary folders... Set Fldrs = Fldr1.SubFolders '-- If 1 = Fldrs.Count Then S = S & "There is 1 folder" & NewLine Else S = S & "There are " & Fldrs.Count & " folders." & NewLine End If ' on error goto 0 'Msgbox "Folders " & S, 64 ' If Fldrs.Count > 0 Then For Each Fldr in Fldrs ' Wscript.Echo Fldr.name Fldr.Delete(1) Next End If '-- on error resume next '-- If Files.Count <> 0 Then For Each File In Files '-- try and delete it... File.Delete(1) '-- strMsg = vbCr & getOwner( File ) ' MsgBox File & vbCr & strMsg, 64, "Cannot delete this file." ' Next End If '-- Set Files = Fldr2.Files '-- If 1 = Files.Count Then ' S = S & "There is 1 file" & NewLine Else ' S = S & "There are " & Files.Count & " files" & NewLine End If '-- on error resume next ' If Files.Count <> 0 Then For Each File In Files '-- try and delete it... File.Delete(1) '-- strMsg = vbCr & getOwner( File ) ' MsgBox File & vbCr & strMsg, 64, "Cannot delete this file." ' Next '-- End If '-- Now we try and delete these temporary folders... Set Fldrs = Fldr2.SubFolders '-- If 1 = Fldrs.Count Then S = S & "There is 1 folder" & NewLine Else S = S & "There are " & Fldrs.Count & " folders." & NewLine End If ' on error resume next 'Msgbox "Folders " & S, 64 ' '-- If Fldrs.Count > 0 Then For Each Fldr in Fldrs '-- Fldr.delete ' If Err Then MsgBox "Cannot delete this folder: " & Fldr.Name, 64 Next End If '-- end of query msgbox End If '-- MsgBox "Program Completed." ,64, WSCript.scriptfullname '-- WScript.Quit 0 '-------> Function getOwner( strFile ) '============================================================================ ' Who owns this file? '============================================================================ ' Dim strRet '-- On Error Resume Next '-- strComputer = "." '-- Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & strFile & "'}" _ & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner") For Each objItem in colItems ' Wscript.Echo objItem.ReferencedDomainName strRet = "Domain owner: " & objItem.ReferencedDomainName & vbCr ' Wscript.Echo objItem.AccountName strRet = strRet & "Account owner: " & objItem.AccountName & vbCr Next '-- return the string getOwner = strRet ' End Function