think tank forum

technology » windows temp files removal

ozntz's avatar
16 years ago
link
ozntz
toooooooooooooooooooooooooooooooooo
I'm trying to script the removal of windows temp files with vb. My question to ttf is how would you descide if it is old or not. My first method was to datediff 2 days based on last modified however I see last accessed is maytimes a different date. What would you pick or multi?
ozntz's avatar
16 years ago
link
ozntz
toooooooooooooooooooooooooooooooooo
'Temp File Clean up - OZntz
'Designed as a Computer Shutdown or startup script running as system account.
'Folders checked - System temp, users temp, users temp internet files
'
Dim Read, WshShell, objfolder, objfile, filenames(), subfolders
Dim Delfilesnum, Delfoldnum, objsubfolders, objsubFile
Dim sAllUsersProfile, oAllUsersFolder, sProfilesRoot, oFolder
strComputer = "." 
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
  strComputer & "\root\default:StdRegProv") 

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

Delfilesnum = "0"
Delfoldnum = "0"
Systemtemp =  WshShell.ExpandEnvironmentStrings("%TEMP%")

sAllUsersProfile    = WshShell.ExpandEnvironmentStrings("%ALLUSERSPROFILE%")
Set oAllUsersFolder = oFSO.GetFolder(sAllUsersProfile)
sProfilesRoot       = oAllUsersFOlder.ParentFolder
Set oFolder            = oFSO.GetFolder(sProfilesRoot)
Set colFSOSubfolders   = oFolder.Subfolders

'On Error Resume Next 

'Enable file log of removed files - Remove comment on next line to enable
'Set testlog = oFSO.OpenTextFile(systemtemp & "\tempfilecleanuplog.txt", 2, True)

'Clean System Temp
Set objfolder = oFSO.GetFolder(systemtemp)
'CleanRoot objfolder
'FindSubFolders objfolder
'WScript.Echo objfolder

'Clean User Temp
For each profilefolder in colFSOSubFolders
IF profilefolder = "C:\Documents and Settings\All Users" THEN
WScript.Echo "Skip"
Else

	IF oFSO.FolderExists (profilefolder & "\Local Settings\Temp") THEN
	Set objfolder = oFSO.GetFolder(profilefolder & "\Local Settings\Temp")
	WScript.Echo objfolder
	CleanRoot objfolder
	FindSubFolders objfolder
	Else
	WScript.Echo "Failed to open Folder", objfolder
	END If

End If
Next


'Clean User Temp Internet Files
For each profilefolder in colFSOSubFolders
IF profilefolder = "C:\Documents and Settings\All Users" THEN
WScript.Echo "Skip"
Else
	IF oFSO.FolderExists (profilefolder & "\Local Settings\Temporary Internet Files") THEN
	Set objfolder = oFSO.GetFolder(profilefolder & "\Local Settings\Temporary Internet Files")
	WScript.Echo objfolder
	CleanRoot objfolder
	FindSubFolders objfolder
	ELSE
	WScript.Echo "Failed to open Folder", objfolder
	END If
End If
Next

'++++++
Function CleanRoot(objfolder)
On Error Resume Next
For each objfile in objfolder.Files
On Error Resume Next
'WScript.Echo objfile
'WScript.Echo DateDiff("d", objFile.DateCreated, Now) > 2 And DateDiff("d",objFile.DateLastModified, Now) > 2
	IF DateDiff("d", objFile.DateCreated, Now) > 2 AND DateDiff("d",objFile.DateLastModified, Now) > 2  Then
										testlog.WriteLine objfile
			'WScript.Echo objfile
			oFSO.DeleteFile objfile, True
	'objfile.Delete
	Delfilesnum = delfilesnum + 1
	END IF
Next
End Function
'++++++



'++++++
Function FindSubFolders(objfolder)
'WScript.Echo objfolder

For each objsubfolders in objfolder.SubFolders
	For each objsubFile in objsubfolders.Files
	On Error Resume Next
	IF DateDiff("d", objsubFile.DateCreated, Now) > "2" AND DateDiff("d", objsubFile.DateLastModified, Now) > "2"  Then
										testlog.WriteLine objsubFile
	oFSO.DeleteFile objfile, True
	delfilesnum = delfilesnum + 1
	END IF
	Next
IF objsubfolders.SubFolders.Count > 0 Then
FindSubFolders objsubfolders
Else
	If objsubfolders.Files.Count = 0 THEN
										testlog.WriteLine objsubFolders
	oFSO.DeleteFolder objsubfolders
	delfoldnum = delfoldnum + 1
	END IF
END IF
Next
End Function
'++++++



testlog.Close

WshShell.LogEvent 4, "Temp File Cleanup has completed. Files to be deleted: " & Delfilesnum & ". Emtpy Folders to be cleaned up: " & Delfoldnum & "."
WScript.Echo "Number of files to be deleted", delfilesnum
WScript.Echo "Number of empty folders to be cleaned up", delfoldnum
WScript.Echo "End"
ozntz's avatar
16 years ago
link
ozntz
toooooooooooooooooooooooooooooooooo
Sorry 2 errors in the previous post

'Temp File Clean up - David
'Designed as a Computer Shutdown or startup script running as system account.
'Folders checked - System temp, users temp, users temp internet files
'
Dim Read, WshShell, objfolder, objfile, filenames(), subfolders
Dim Delfilesnum, Delfoldnum, objsubfolders, objsubFile
Dim sAllUsersProfile, oAllUsersFolder, sProfilesRoot, oFolder
strComputer = "." 
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
  strComputer & "\root\default:StdRegProv") 

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

Delfilesnum = "0"
Delfoldnum = "0"
Systemtemp =  WshShell.ExpandEnvironmentStrings("%TEMP%")

sAllUsersProfile    = WshShell.ExpandEnvironmentStrings("%ALLUSERSPROFILE%")
Set oAllUsersFolder = oFSO.GetFolder(sAllUsersProfile)
sProfilesRoot       = oAllUsersFOlder.ParentFolder
Set oFolder            = oFSO.GetFolder(sProfilesRoot)
Set colFSOSubfolders   = oFolder.Subfolders

On Error Resume Next 

'Enable file log of removed files
'Set testlog = oFSO.OpenTextFile(systemtemp & "\tempfilecleanuplog.txt", 2, True)

'Clean System Temp
Set objfolder = oFSO.GetFolder(systemtemp)
'CleanRoot objfolder
'FindSubFolders objfolder
'WScript.Echo objfolder

'Clean User Temp
For each profilefolder in colFSOSubFolders
IF profilefolder = "C:\Documents and Settings\All Users" THEN
WScript.Echo "Skip"
Else

	IF oFSO.FolderExists (profilefolder & "\Local Settings\Temp") THEN
	Set objfolder = oFSO.GetFolder(profilefolder & "\Local Settings\Temp")
	WScript.Echo objfolder
	CleanRoot objfolder
	FindSubFolders objfolder
	Else
	WScript.Echo "Failed to open Folder", objfolder
	END If

End If
Next


'Clean User Temp Internet Files
For each profilefolder in colFSOSubFolders
IF profilefolder = "C:\Documents and Settings\All Users" THEN
WScript.Echo "Skip"
Else
	IF oFSO.FolderExists (profilefolder & "\Local Settings\Temporary Internet Files") THEN
	Set objfolder = oFSO.GetFolder(profilefolder & "\Local Settings\Temporary Internet Files")
	WScript.Echo objfolder
	CleanRoot objfolder
	FindSubFolders objfolder
	ELSE
	WScript.Echo "Failed to open Folder", objfolder
	END If
End If
Next

'++++++
Function CleanRoot(objfolder)
On Error Resume Next
For each objfile in objfolder.Files
On Error Resume Next
'WScript.Echo objfile
'WScript.Echo DateDiff("d", objFile.DateCreated, Now) > 2 And DateDiff("d",objFile.DateLastModified, Now) > 2
	IF DateDiff("d", objFile.DateCreated, Now) > 2 AND DateDiff("d",objFile.DateLastModified, Now) > 2  Then
										testlog.WriteLine objfile
			'WScript.Echo objfile
			oFSO.DeleteFile objfile, True
	'objfile.Delete
	Delfilesnum = delfilesnum + 1
	END IF
Next
End Function
'++++++



'++++++
Function FindSubFolders(objfolder)
'WScript.Echo objfolder

For each objsubfolders in objfolder.SubFolders
	For each objsubFile in objsubfolders.Files
	On Error Resume Next
	IF DateDiff("d", objsubFile.DateCreated, Now) > "2" AND DateDiff("d", objsubFile.DateLastModified, Now) > "2"  Then
										testlog.WriteLine objsubFile
	oFSO.DeleteFile objsubFile, True
	delfilesnum = delfilesnum + 1
	END IF
	Next
IF objsubfolders.SubFolders.Count > 0 Then
FindSubFolders objsubfolders
Else
	If objsubfolders.Files.Count = 0 THEN
										testlog.WriteLine objsubFolders
	oFSO.DeleteFolder objsubfolders
	delfoldnum = delfoldnum + 1
	END IF
END IF
Next
End Function
'++++++



testlog.Close

WshShell.LogEvent 4, "Temp File Cleanup has completed. Files to be deleted: " & Delfilesnum & ". Emtpy Folders to be cleaned up: " & Delfoldnum & "."
WScript.Echo "Number of files to be deleted", delfilesnum
WScript.Echo "Number of empty folders to be cleaned up", delfoldnum
WScript.Echo "End"