Solved

Script to delete the temp files and internet temp files on any OS when run. Win Xp,vista,2003,2008.

Posted on 2008-06-14
47
1,682 Views
Last Modified: 2012-06-21
Hi,

Script to delete the temp files and internet temp files on any OS when run. Win Xp,vista,2003,2008.
This should be possible locally and remotely through c$

Regards'
Sharath
0
Comment
Question by:bsharath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 34
  • 12
47 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 21790160
Sharath, this should work locally, try it out.  If it does work properly for you, I'll try to modify it to clear remotely as well, based on the current user logged in.

Regards,

Rob.
'======== START OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
' Delete the temporary internet files
' SET boolClearCache to True to clear cache before downloading, or set it
' to False to not clear the cache
 
'======== METHOD 1 ==================
boolClearCache = True
If boolClearCache = True Then
	Const TEMPORARY_INTERNET_FILES = &H20&
	
	Set objShell = CreateObject("Shell.Application")
	Set objFolder = objShell.Namespace(TEMPORARY_INTERNET_FILES)
	Set objFolderItem = objFolder.Self
	strPath = objFolderItem.Path & "\*.*"
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	objFSO.DeleteFile(strPath)
	Set objFSO = Nothing
End If
 
'======== METHOD 2 ===================
boolClearCache = True
If boolClearCache = True Then
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("WScript.Shell")
	strTempFiles = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Local Settings\Temporary Internet Files"
	strTempFiles = objFSO.GetFolder(strTempFiles).ShortPath
	strCacheLog = "Cache Files Cleared" & VbCrLf & "==================="
	For Each objFile In objFSO.GetFolder(strTempFiles).Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next	
	For Each objSubFolder In objFSO.GetFolder(strTempFiles).SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
	Set objCacheLog = objFSO.CreateTextFile("CacheDeleteLog.txt", True)
	objCacheLog.Write strCacheLog
	objCacheLog.Close
	Set objCacheLog = Nothing
	Set objFSO = Nothing
	Set objShell = Nothing
End If
 
'======== END OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 21790705
Rob i get this
---------------------------
Windows Script Host
---------------------------
Script:      C:\Temp deletion.vbs
Line:      42
Char:      3
Error:      Type mismatch: 'RecurseSubFolders'
Code:      800A000D
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
LVL 11

Author Comment

by:bsharath
ID: 21790706
Rob i get this
---------------------------
Windows Script Host
---------------------------
Script:      C:\Temp deletion.vbs
Line:      42
Char:      3
Error:      Type mismatch: 'RecurseSubFolders'
Code:      800A000D
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Expert Comment

by:RobSampson
ID: 21790752
Oh whoops, that wasn't complete...

Rob.
'======== START OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
' Delete the temporary internet files
' SET boolClearCache to True to clear cache before downloading, or set it
' to False to not clear the cache
 
'======== METHOD 1 ==================
boolClearCache = True
If boolClearCache = True Then
	Const TEMPORARY_INTERNET_FILES = &H20&
	
	Set objShell = CreateObject("Shell.Application")
	Set objFolder = objShell.Namespace(TEMPORARY_INTERNET_FILES)
	Set objFolderItem = objFolder.Self
	strPath = objFolderItem.Path & "\*.*"
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	objFSO.DeleteFile(strPath)
	Set objFSO = Nothing
End If
 
'======== METHOD 2 ===================
boolClearCache = True
If boolClearCache = True Then
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("WScript.Shell")
	strTempFiles = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Local Settings\Temporary Internet Files"
	strTempFiles = objFSO.GetFolder(strTempFiles).ShortPath
	strCacheLog = "Cache Files Cleared" & VbCrLf & "==================="
	For Each objFile In objFSO.GetFolder(strTempFiles).Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next	
	For Each objSubFolder In objFSO.GetFolder(strTempFiles).SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
	Set objCacheLog = objFSO.CreateTextFile("CacheDeleteLog.txt", True)
	objCacheLog.Write strCacheLog
	objCacheLog.Close
	Set objCacheLog = Nothing
	Set objFSO = Nothing
	Set objShell = Nothing
End If
 
'======== END OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
 
Sub RecurseSubFolders(objFolder)
	For Each objFile In objFolder.Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next
 
	For Each objSubFolder In objFolder.SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
End Sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 21790772
Rob i got a message as access is denied but the files were cleared...
Will this work on all versions of OS?
Can the same script be used remotely?
0
 
LVL 11

Author Comment

by:bsharath
ID: 21790773
Rob i got a message as access is denied but the files were cleared...
Will this work on all versions of OS?
Can the same script be used remotely?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21790827
It should work on all versions....they all have the TEMPORARY_INTERNET_FILES namespace and they all have the files in \Local Settings\Temporary Internet Files

I have added error checking to get rid of the Access Denied message.

I will work on executing it remotely....

Rob.
'======== START OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
' Delete the temporary internet files
' SET boolClearCache to True to clear cache before downloading, or set it
' to False to not clear the cache
 
'======== METHOD 1 ==================
boolClearCache = True
If boolClearCache = True Then
	Const TEMPORARY_INTERNET_FILES = &H20&
	
	Set objShell = CreateObject("Shell.Application")
	Set objFolder = objShell.Namespace(TEMPORARY_INTERNET_FILES)
	Set objFolderItem = objFolder.Self
	strPath = objFolderItem.Path & "\*.*"
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	On Error Resume Next
	objFSO.DeleteFile(strPath)
	Err.Clear
	On Error GoTo 0
	Set objFSO = Nothing
End If
 
'======== METHOD 2 ===================
boolClearCache = True
If boolClearCache = True Then
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("WScript.Shell")
	strTempFiles = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Local Settings\Temporary Internet Files"
	strTempFiles = objFSO.GetFolder(strTempFiles).ShortPath
	strCacheLog = "Cache Files Cleared" & VbCrLf & "==================="
	For Each objFile In objFSO.GetFolder(strTempFiles).Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next	
	For Each objSubFolder In objFSO.GetFolder(strTempFiles).SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
	Set objCacheLog = objFSO.CreateTextFile("CacheDeleteLog.txt", True)
	objCacheLog.Write strCacheLog
	objCacheLog.Close
	Set objCacheLog = Nothing
	Set objFSO = Nothing
	Set objShell = Nothing
End If
 
'======== END OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
 
Sub RecurseSubFolders(objFolder)
	For Each objFile In objFolder.Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next
 
	For Each objSubFolder In objFolder.SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
End Sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 21790850
Rob i get this 1 file as failure
FAILURE: C:\DOCUME~1\ADMINI~1.DEV\LOCALS~1\TEMPOR~1\Content.IE5\index.dat
0
 
LVL 11

Author Comment

by:bsharath
ID: 21790851
Rob i get this 1 file as failure
FAILURE: C:\DOCUME~1\ADMINI~1.DEV\LOCALS~1\TEMPOR~1\Content.IE5\index.dat
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21790861
Yeah, that's normal.  You won't be able to delete that file while the user is logged in, because it is constantly in use.  Don't worry about that though, it's only very small....

Rob.
0
 
LVL 11

Author Comment

by:bsharath
ID: 21790880
Ok Rob...
Does the scruipt delete the temp and temp internet files?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21791000
It should now, try this one, it can work locally and remotely....

If you do not get the ClearCacheLog.txt file updated, then that would mean there is probably a permissions issue, so try re-entering the credentials, or it cannot find the remote folder....

You need to change this bit:
\\mccdc01\netlogon\psexec_182.exe

to match the file path to a version of PSExec.exe that you have.

Rob.
Option Explicit
 
Dim objShell, objNetwork, objProcess, strUserName, strHostName, strCommand, strAdminUser, strAdminPass, strComputer
Dim objWMIService, colComputer, objComputer, strRemoteUser, boolClearCache, objFSO, strLoggedInUser, strTempFiles
Dim strCacheLog, objFile, strFilePath, objSubFolder, objCacheLog
 
If Left(WScript.ScriptFullName, 2) <> "\\" Then
	MsgBox "Please run this script from a UNC path."
	WScript.Quit
End If
 
Set objShell = CreateObject("Wscript.Shell")
Set objProcess = objShell.Environment("Process")
 
strUserName = objProcess("USERNAME")
strHostName = objProcess("COMPUTERNAME")
 
If WScript.Arguments.Count < 1 Then
	Call Normal_User_Commands
ElseIf WScript.Arguments(0) = "AsAdmin" Then
	Call Admin_User_Commands
Else
	MsgBox "Unknown Argument received"
End If
 
Sub Normal_User_Commands
	' Enter your Administrative credentials here
	strAdminUser = InputBox("Enter user name:", "User name", "DEVELOPMENT\Administrator")
	strAdminPass = InputBox("Enter the password for " & strAdminUser & ":", "Password")
	
    'MsgBox "Running as initiating user"
    strComputer = "."
    '***********************  THIS MSGBOX CAN BE REMOVED FOR A LOGON SCRIPT *******************
    ' If this MsgBox is used, the admin commands can be run on a remote machine.
    strComputer = InputBox("Enter computer to clear Temporary Internet Files on:", "Enter Computer", "172.16.2.64")
    '******************************************************************************************
	If Ping(strComputer) = False Then
		MsgBox strComputer & " did not respond to ping. Cannot continue script."
	Else
	
		Set objWMIService = GetObject("winmgmts:" _
		    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
		
		Set colComputer = objWMIService.ExecQuery _
		    ("Select * from Win32_ComputerSystem")
		For Each objComputer in colComputer
	    	strRemoteUser = Replace(objComputer.UserName, "\", ".")
	    Next
	    If strRemoteUser = "" Then
	    	MsgBox "No user is currently logged in on " & strComputer & ". Cannot continue script."
		Else
		    ' This command assumes that PSExec.exe (available from Microsoft) is stored on the shared folder
		    ' below.  It runs PSExec as the Admin user, on a remote machine, or the current machine if it is a
		    ' logon script.  It re-runs this file again, this time passing the "AsAdmin" argument, so the script
		    ' knows it has been run under an Admin account, and can safely execute the Admin_User_Commands Sub.
		    ' NOTE: If the Admin is running this script from a mapped drive, the client must have the same drive mapped,
		    ' because of the use of WScript.ScriptFullName below.  It is best to run this script from a UNC path,
		    ' so the client machine can access the file via a UNC path and not rely on a mapped drive.
			strCommand = "cmd /c \\mccdc01\netlogon\psexec_182.exe -accepteula -i -e -u " & strAdminUser & " -p " & strAdminPass & " \\" & strComputer & " WScript """ & WScript.ScriptFullName & """ AsAdmin " & strRemoteUser
			objShell.Run strCommand, 0, True
		    ' NOTE: In the above two lines, you can change the cmd /c to cmd /k and the strCommand, 0, True to strCommand, 1, True
		    ' if you wish to see some output for debugging / testing purposes.
	    End If
	End If
 
End Sub
 
Sub Admin_User_Commands
	' Now the script has detected that "AsAdmin" was passed to it, and will run these commands.
	' Now running as Administrator on the target macchine
	' MsgBox "Running as Admin"
 
	'======== START OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
	' Delete the temporary internet files
	' SET boolClearCache to True to clear cache before downloading, or set it
	' to False to not clear the cache
	
	boolClearCache = True
	If boolClearCache = True Then
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objShell = CreateObject("WScript.Shell")
 
		strLoggedInUser = WScript.Arguments.Item(1)
		strTempFiles = objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\" & strLoggedInUser & "\Local Settings\Temporary Internet Files"
		If objFSO.FolderExists(strTempFiles) = False Then
			If InStr(strLoggedInUser, ".") > 0 Then
				strLoggedInUser = Mid(strLoggedInUser, InStr(strLoggedInUser, ".") + 1)
				strTempFiles = objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\" & strLoggedInUser & "\Local Settings\Temporary Internet Files"
				If objFSO.FolderExists(strTempFiles) = False Then
					'MsgBox "Could not find " & strTempFiles
					WScript.Quit
				End If
			Else
				'MsgBox "Could not find " & strTempFiles
				WScript.Quit
			End If
		End If
		DeleteFilesAtPath strTempFiles
 
		strLoggedInUser = WScript.Arguments.Item(1)
		strTempFiles = objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\" & strLoggedInUser & "\Local Settings\Temp"
		If objFSO.FolderExists(strTempFiles) = False Then
			If InStr(strLoggedInUser, ".") > 0 Then
				strLoggedInUser = Mid(strLoggedInUser, InStr(strLoggedInUser, ".") + 1)
				strTempFiles = objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\" & strLoggedInUser & "\Local Settings\Temp"
				If objFSO.FolderExists(strTempFiles) = False Then
					'MsgBox "Could not find " & strTempFiles
					WScript.Quit
				End If
			Else
				'MsgBox "Could not find " & strTempFiles
				WScript.Quit
			End If
		End If
		DeleteFilesAtPath strTempFiles
 
	End If	
 
End Sub
'======== END OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
 
Sub DeleteFilesAtPath(strThePath)
	strTempFiles = objFSO.GetFolder(strThePath).ShortPath
 
'======== METHOD 1 ===================
	strCacheLog = "Cache Files Cleared on " & objShell.ExpandEnvironmentStrings("%COMPUTERNAME%") & ": " & Now & VbCrLf & "========================================"
	For Each objFile In objFSO.GetFolder(strTempFiles).Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next	
	For Each objSubFolder In objFSO.GetFolder(strTempFiles).SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
	Set objCacheLog = objFSO.OpenTextFile(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ClearCacheLog.txt", 8, True)
	objCacheLog.Write VbCrLf & VbCrLf & strCacheLog
	objCacheLog.Close
	Set objCacheLog = Nothing
'======== METHOD 2 ==================
	On Error Resume Next
	objFSO.DeleteFile(strTempFiles & "\*.*")
	Err.Clear
	On Error GoTo 0
End Sub
 
Sub RecurseSubFolders(objFolder)
	For Each objFile In objFolder.Files
		On Error Resume Next
		strFilePath = objFile.Path
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next
 
	For Each objSubFolder In objFolder.SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
End Sub
'=======================
 
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 21791042
Rob i get the file created. For some machines the file does not create. So can we have success and failure report.
Can i mention a file that has the machine names listed ?
0
 
LVL 11

Author Comment

by:bsharath
ID: 21791043
Rob i get the file created. For some machines the file does not create. So can we have success and failure report.
Can i mention a file that has the machine names listed ?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21791148
Try this, and make sure you change the PSExec path....

It's not one of the cleanest scripts I've ever written....it's kind of a rehashed old script, so it's a bit messy.  Making it take a list of computer names from from a file would probably make it too messy...

Rob.
Option Explicit
 
Dim objShell, objNetwork, objProcess, strUserName, strHostName, strCommand, strAdminUser, strAdminPass, strComputer
Dim objWMIService, colComputer, objComputer, strRemoteUser, boolClearCache, objFSO, strLoggedInUser, strTempFiles
Dim strCacheLog, objFile, strFilePath, objSubFolder, objCacheLog, boolFound
 
If Left(WScript.ScriptFullName, 2) <> "\\" Then
	MsgBox "Please run this script from a UNC path."
	WScript.Quit
End If
 
Set objShell = CreateObject("Wscript.Shell")
Set objProcess = objShell.Environment("Process")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strUserName = objProcess("USERNAME")
strHostName = objProcess("COMPUTERNAME")
 
If WScript.Arguments.Count < 1 Then
	Call Normal_User_Commands
ElseIf WScript.Arguments(0) = "AsAdmin" Then
	Call Admin_User_Commands
Else
	MsgBox "Unknown Argument received"
End If
 
Sub Normal_User_Commands
	' Enter your Administrative credentials here
	strAdminUser = InputBox("Enter user name:", "User name", "DEVELOPMENT\Administrator")
	strAdminPass = InputBox("Enter the password for " & strAdminUser & ":", "Password")
	
    'MsgBox "Running as initiating user"
    strComputer = "."
    '***********************  THIS MSGBOX CAN BE REMOVED FOR A LOGON SCRIPT *******************
    ' If this MsgBox is used, the admin commands can be run on a remote machine.
    strComputer = InputBox("Enter computer to clear Temporary Internet Files on:", "Enter Computer", "172.16.2.64")
    '******************************************************************************************
	If Ping(strComputer) = False Then
		MsgBox strComputer & " did not respond to ping. Cannot continue script."
	Else
	
		Set objWMIService = GetObject("winmgmts:" _
		    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
		
		Set colComputer = objWMIService.ExecQuery _
		    ("Select * from Win32_ComputerSystem")
		For Each objComputer in colComputer
	    	strRemoteUser = Replace(objComputer.UserName, "\", ".")
	    Next
	    If strRemoteUser = "" Then
	    	MsgBox "No user is currently logged in on " & strComputer & ". Cannot continue script."
		Else
		    ' This command assumes that PSExec.exe (available from Microsoft) is stored on the shared folder
		    ' below.  It runs PSExec as the Admin user, on a remote machine, or the current machine if it is a
		    ' logon script.  It re-runs this file again, this time passing the "AsAdmin" argument, so the script
		    ' knows it has been run under an Admin account, and can safely execute the Admin_User_Commands Sub.
		    ' NOTE: If the Admin is running this script from a mapped drive, the client must have the same drive mapped,
		    ' because of the use of WScript.ScriptFullName below.  It is best to run this script from a UNC path,
		    ' so the client machine can access the file via a UNC path and not rely on a mapped drive.
			WriteToLog " *** *** Starting Script against " & strRemoteUser & " on " & strComputer & ": " & Now
			strCommand = "cmd /c \\mccdc01\netlogon\psexec_182.exe -accepteula -i -e -u " & strAdminUser & " -p " & strAdminPass & " \\" & strComputer & " WScript """ & WScript.ScriptFullName & """ AsAdmin " & strRemoteUser
			objShell.Run strCommand, 0, True
		    ' NOTE: In the above two lines, you can change the cmd /c to cmd /k and the strCommand, 0, True to strCommand, 1, True
		    ' if you wish to see some output for debugging / testing purposes.
	    End If
	End If
 
End Sub
 
Sub Admin_User_Commands
	' Now the script has detected that "AsAdmin" was passed to it, and will run these commands.
	' Now running as Administrator on the target macchine
	' MsgBox "Running as Admin"
 
	'======== START OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
	' Delete the temporary internet files
	' SET boolClearCache to True to clear cache before downloading, or set it
	' to False to not clear the cache
	
	boolClearCache = True
	If boolClearCache = True Then
 
		boolFound = False
		strLoggedInUser = WScript.Arguments.Item(1)
		strTempFiles = objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\" & strLoggedInUser & "\Local Settings\Temporary Internet Files"
		If objFSO.FolderExists(strTempFiles) = False Then
			If InStr(strLoggedInUser, ".") > 0 Then
				strLoggedInUser = Mid(strLoggedInUser, InStr(strLoggedInUser, ".") + 1)
				strTempFiles = objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\" & strLoggedInUser & "\Local Settings\Temporary Internet Files"
				If objFSO.FolderExists(strTempFiles) = False Then
					'MsgBox "Could not find " & strTempFiles
					WriteToLog "Could not find " & strTempFiles
					'WScript.Quit
				Else
					boolFound = True
				End If
			Else
				'MsgBox "Could not find " & strTempFiles
				WriteToLog "Could not find " & strTempFiles
				'WScript.Quit
			End If
		Else
			boolFound = True
		End If
		
		If boolFound = True Then DeleteFilesAtPath strTempFiles
 
 		boolFound = False
		strLoggedInUser = WScript.Arguments.Item(1)
		strTempFiles = objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\" & strLoggedInUser & "\Local Settings\Temp"
		If objFSO.FolderExists(strTempFiles) = False Then
			If InStr(strLoggedInUser, ".") > 0 Then
				strLoggedInUser = Mid(strLoggedInUser, InStr(strLoggedInUser, ".") + 1)
				strTempFiles = objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\" & strLoggedInUser & "\Local Settings\Temp"
				If objFSO.FolderExists(strTempFiles) = False Then
					'MsgBox "Could not find " & strTempFiles
					WriteToLog "Could not find " & strTempFiles
					'WScript.Quit
				Else
					boolFound = True
				End If
			Else
				'MsgBox "Could not find " & strTempFiles
				WriteToLog "Could not find " & strTempFiles
				'WScript.Quit
			End If
		Else
			boolFound = True
		End If
		
		If boolFound = True Then DeleteFilesAtPath strTempFiles
 
	End If	
 
End Sub
'======== END OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
 
Sub DeleteFilesAtPath(strThePath)
	strTempFiles = objFSO.GetFolder(strThePath).ShortPath
 
'======== METHOD 1 ===================
	strCacheLog = "Cache Files Cleared on " & objShell.ExpandEnvironmentStrings("%COMPUTERNAME%") & ": " & Now & VbCrLf & "========================================"
	For Each objFile In objFSO.GetFolder(strTempFiles).Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next	
	For Each objSubFolder In objFSO.GetFolder(strTempFiles).SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
	WriteToLog strCacheLog
'======== METHOD 2 ==================
	On Error Resume Next
	objFSO.DeleteFile(strTempFiles & "\*.*")
	Err.Clear
	On Error GoTo 0
	WriteToLog "Files deleted from " & strTempFiles
End Sub
 
Sub RecurseSubFolders(objFolder)
	For Each objFile In objFolder.Files
		On Error Resume Next
		strFilePath = objFile.Path
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next
 
	For Each objSubFolder In objFolder.SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
End Sub
'=======================
 
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function
 
Sub WriteToLog(strText)
	Set objCacheLog = objFSO.OpenTextFile(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ClearCacheLog.txt", 8, True)
	objCacheLog.Write VbCrLf & VbCrLf & strText
	objCacheLog.Close
	Set objCacheLog = Nothing
End Sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 21791268
Thanks Rob
Can we atleast have the machine names ask me until i click cancel.
So i can add as many machine names as i want...
This is basically for my Sophos that send me loads of mails stating there was a virus found in there but not able to delete them...
0
 
LVL 11

Author Comment

by:bsharath
ID: 21791269
Thanks Rob
Can we atleast have the machine names ask me until i click cancel.
So i can add as many machine names as i want...
This is basically for my Sophos that send me loads of mails stating there was a virus found in there but not able to delete them...
0
 
LVL 11

Author Comment

by:bsharath
ID: 21791287
Rob for some servers i get this...

---------------------------
Windows Script Host
---------------------------
Script:      \\sophos\c$\Delete temp remote also.vbs
Line:      48
Char:      7
Error:      Invalid use of Null: 'Replace'
Code:      800A005E
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
LVL 11

Author Comment

by:bsharath
ID: 21791592
Rob can i get a "Done" box once the deletion is over.
Some OS we have "C:\WINDOWS\Temp\" does the script delete there also.
Does it delete internet files just from the profile thats currently logged in or all profiles.
If just the one currently logged in can you please change it to all profiles...
0
 
LVL 11

Author Comment

by:bsharath
ID: 21791593
Rob can i get a "Done" box once the deletion is over.
Some OS we have "C:\WINDOWS\Temp\" does the script delete there also.
Does it delete internet files just from the profile thats currently logged in or all profiles.
If just the one currently logged in can you please change it to all profiles...
0
 
LVL 11

Author Comment

by:bsharath
ID: 21799429
Rob a reminder when free....
0
 
LVL 11

Author Comment

by:bsharath
ID: 21799430
Rob a reminder when free....
0
 
LVL 11

Author Comment

by:bsharath
ID: 21823333
Any help on this Rob and did you have a chance to check you mail (Hotmail).?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21828311
Ok, try this for
a) taking input from computers.txt
b) deleting files from all user profiles
c) prompting you after each machine has been done
d) deleting files from C:\Windows\Temp

I haven't tested it yet, so you might have an error or two when you first run it....

Regards,

Rob.
Option Explicit
 
Dim objShell, objNetwork, objProcess, strUserName, strHostName, strCommand, strAdminUser, strAdminPass, strComputer
Dim objWMIService, colComputer, objComputer, boolClearCache, objFSO, strTempFiles
Dim strCacheLog, objInputFile, objFile, strFilePath, objSubFolder, objCacheLog, boolFound
Const intForReading = 1
 
If Left(WScript.ScriptFullName, 2) <> "\\" Then
	MsgBox "Please run this script from a UNC path."
	WScript.Quit
End If
 
Set objShell = CreateObject("Wscript.Shell")
Set objProcess = objShell.Environment("Process")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strUserName = objProcess("USERNAME")
strHostName = objProcess("COMPUTERNAME")
 
If WScript.Arguments.Count < 1 Then
	Call Normal_User_Commands
ElseIf WScript.Arguments(0) = "AsAdmin" Then
	Call Admin_User_Commands
Else
	MsgBox "Unknown Argument received"
End If
 
Sub Normal_User_Commands
	' Enter your Administrative credentials here
	strAdminUser = InputBox("Enter user name:", "User name", "DEVELOPMENT\Administrator")
	strAdminPass = InputBox("Enter the password for " & strAdminUser & ":", "Password")
	
    'MsgBox "Running as initiating user"
    'strComputer = "."
    Set objInputFile = objFSO.OpenTextFile("Computers.txt", intForReading, False)
    While Not objInputFile.AtEndOfStream
	    '***********************  THIS MSGBOX CAN BE REMOVED FOR A LOGON SCRIPT *******************
	    ' If this MsgBox is used, the admin commands can be run on a remote machine.
	    'strComputer = InputBox("Enter computer to clear Temporary Internet Files on:", "Enter Computer", "172.16.2.64")
	    '******************************************************************************************
		strComputer = objInputFile.ReadLine
		If Ping(strComputer) = False Then
			'MsgBox strComputer & " did not respond to ping. Cannot continue script."
			WriteToLog strComputer & " did not respond to ping. Cannot continue script."
		Else
		
			Set objWMIService = GetObject("winmgmts:" _
			    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
			
			Set colComputer = objWMIService.ExecQuery _
			    ("Select * from Win32_ComputerSystem")
	
		    ' This command assumes that PSExec.exe (available from Microsoft) is stored on the shared folder
		    ' below.  It runs PSExec as the Admin user, on a remote machine, or the current machine if it is a
		    ' logon script.  It re-runs this file again, this time passing the "AsAdmin" argument, so the script
		    ' knows it has been run under an Admin account, and can safely execute the Admin_User_Commands Sub.
		    ' NOTE: If the Admin is running this script from a mapped drive, the client must have the same drive mapped,
		    ' because of the use of WScript.ScriptFullName below.  It is best to run this script from a UNC path,
		    ' so the client machine can access the file via a UNC path and not rely on a mapped drive.
			WriteToLog " *** *** Starting Script against " & strRemoteUser & " on " & strComputer & ": " & Now
			strCommand = "cmd /c \\mccdc01\netlogon\psexec_182.exe -accepteula -i -e -u " & strAdminUser & " -p " & strAdminPass & " \\" & strComputer & " WScript """ & WScript.ScriptFullName & """ AsAdmin"
			objShell.Run strCommand, 0, True
		    ' NOTE: In the above two lines, you can change the cmd /c to cmd /k and the strCommand, 0, True to strCommand, 1, True
		    ' if you wish to see some output for debugging / testing purposes.
		    MsgBox "Finished running on " & strComputer
	Wend
 
End Sub
 
Sub Admin_User_Commands
	' Now the script has detected that "AsAdmin" was passed to it, and will run these commands.
	' Now running as Administrator on the target macchine
	' MsgBox "Running as Admin"
 
	For Each objSubFolder In objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\").SubFolders
		boolFound = False
		strTempFiles = objSubFolder.Path & "\Local Settings\Temporary Internet Files"
		If objFSO.FolderExists(strTempFiles) = False Then
			'MsgBox "Could not find " & strTempFiles
			WriteToLog "Could not find " & strTempFiles
			'WScript.Quit
		Else
			boolFound = True
		End If
		
		If boolFound = True Then DeleteFilesAtPath strTempFiles
	Next
 
	For Each objSubFolder In objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\").SubFolders
		boolFound = False
		strTempFiles = objSubFolder.Path & "\Local Settings\Temp"
		If objFSO.FolderExists(strTempFiles) = False Then
			'MsgBox "Could not find " & strTempFiles
			WriteToLog "Could not find " & strTempFiles
			'WScript.Quit
		Else
			boolFound = True
		End If
		
		If boolFound = True Then DeleteFilesAtPath strTempFiles
	Next
 
	boolFound = False
	strTempFiles = objShell.ExpandEnvironmentStrings("%WINDIR%") & "\Temp"
	If objFSO.FolderExists(strTempFiles) = False Then
		'MsgBox "Could not find " & strTempFiles
		WriteToLog "Could not find " & strTempFiles
		'WScript.Quit
	Else
		boolFound = True
	End If
	
	If boolFound = True Then DeleteFilesAtPath strTempFiles
 
End Sub
'======== END OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
 
Sub DeleteFilesAtPath(strThePath)
	strTempFiles = objFSO.GetFolder(strThePath).ShortPath
 
'======== METHOD 1 ===================
	strCacheLog = "Cache Files Cleared on " & objShell.ExpandEnvironmentStrings("%COMPUTERNAME%") & ": " & Now & VbCrLf & "========================================"
	For Each objFile In objFSO.GetFolder(strTempFiles).Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next	
	For Each objSubFolder In objFSO.GetFolder(strTempFiles).SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
	WriteToLog strCacheLog
'======== METHOD 2 ==================
	On Error Resume Next
	objFSO.DeleteFile(strTempFiles & "\*.*")
	Err.Clear
	On Error GoTo 0
	WriteToLog "Files deleted from " & strTempFiles
End Sub
 
Sub RecurseSubFolders(objFolder)
	For Each objFile In objFolder.Files
		On Error Resume Next
		strFilePath = objFile.Path
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next
 
	For Each objSubFolder In objFolder.SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
End Sub
'=======================
 
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function
 
Sub WriteToLog(strText)
	Set objCacheLog = objFSO.OpenTextFile(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ClearCacheLog.txt", 8, True)
	objCacheLog.Write VbCrLf & VbCrLf & strText
	objCacheLog.Close
	Set objCacheLog = Nothing
End Sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 21828342
Thanks Rob

I get this
---------------------------
Windows Script Host
---------------------------
Script:      C:\Delete Temp Latest.vbs
Line:      66
Char:      2
Error:      Expected statement
Code:      800A0400
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828343
Thanks Rob

I get this
---------------------------
Windows Script Host
---------------------------
Script:      C:\Delete Temp Latest.vbs
Line:      66
Char:      2
Error:      Expected statement
Code:      800A0400
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21828385
Missed an End If...
Option Explicit
 
Dim objShell, objNetwork, objProcess, strUserName, strHostName, strCommand, strAdminUser, strAdminPass, strComputer
Dim objWMIService, colComputer, objComputer, boolClearCache, objFSO, strTempFiles
Dim strCacheLog, objInputFile, objFile, strFilePath, objSubFolder, objCacheLog, boolFound
Const intForReading = 1
 
If Left(WScript.ScriptFullName, 2) <> "\\" Then
	MsgBox "Please run this script from a UNC path."
	WScript.Quit
End If
 
Set objShell = CreateObject("Wscript.Shell")
Set objProcess = objShell.Environment("Process")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strUserName = objProcess("USERNAME")
strHostName = objProcess("COMPUTERNAME")
 
If WScript.Arguments.Count < 1 Then
	Call Normal_User_Commands
ElseIf WScript.Arguments(0) = "AsAdmin" Then
	Call Admin_User_Commands
Else
	MsgBox "Unknown Argument received"
End If
 
Sub Normal_User_Commands
	' Enter your Administrative credentials here
	strAdminUser = InputBox("Enter user name:", "User name", "DEVELOPMENT\Administrator")
	strAdminPass = InputBox("Enter the password for " & strAdminUser & ":", "Password")
	
    'MsgBox "Running as initiating user"
    'strComputer = "."
    Set objInputFile = objFSO.OpenTextFile("Computers.txt", intForReading, False)
    While Not objInputFile.AtEndOfStream
	    '***********************  THIS MSGBOX CAN BE REMOVED FOR A LOGON SCRIPT *******************
	    ' If this MsgBox is used, the admin commands can be run on a remote machine.
	    'strComputer = InputBox("Enter computer to clear Temporary Internet Files on:", "Enter Computer", "172.16.2.64")
	    '******************************************************************************************
		strComputer = objInputFile.ReadLine
		If Ping(strComputer) = False Then
			'MsgBox strComputer & " did not respond to ping. Cannot continue script."
			WriteToLog strComputer & " did not respond to ping. Cannot continue script."
		Else
		
			Set objWMIService = GetObject("winmgmts:" _
			    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
			
			Set colComputer = objWMIService.ExecQuery _
			    ("Select * from Win32_ComputerSystem")
	
		    ' This command assumes that PSExec.exe (available from Microsoft) is stored on the shared folder
		    ' below.  It runs PSExec as the Admin user, on a remote machine, or the current machine if it is a
		    ' logon script.  It re-runs this file again, this time passing the "AsAdmin" argument, so the script
		    ' knows it has been run under an Admin account, and can safely execute the Admin_User_Commands Sub.
		    ' NOTE: If the Admin is running this script from a mapped drive, the client must have the same drive mapped,
		    ' because of the use of WScript.ScriptFullName below.  It is best to run this script from a UNC path,
		    ' so the client machine can access the file via a UNC path and not rely on a mapped drive.
			WriteToLog " *** *** Starting Script against " & strRemoteUser & " on " & strComputer & ": " & Now
			strCommand = "cmd /c \\mccdc01\netlogon\psexec_182.exe -accepteula -i -e -u " & strAdminUser & " -p " & strAdminPass & " \\" & strComputer & " WScript """ & WScript.ScriptFullName & """ AsAdmin"
			objShell.Run strCommand, 0, True
		    ' NOTE: In the above two lines, you can change the cmd /c to cmd /k and the strCommand, 0, True to strCommand, 1, True
		    ' if you wish to see some output for debugging / testing purposes.
		    MsgBox "Finished running on " & strComputer
		End If
	Wend
 
End Sub
 
Sub Admin_User_Commands
	' Now the script has detected that "AsAdmin" was passed to it, and will run these commands.
	' Now running as Administrator on the target macchine
	' MsgBox "Running as Admin"
 
	For Each objSubFolder In objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\").SubFolders
		boolFound = False
		strTempFiles = objSubFolder.Path & "\Local Settings\Temporary Internet Files"
		If objFSO.FolderExists(strTempFiles) = False Then
			'MsgBox "Could not find " & strTempFiles
			WriteToLog "Could not find " & strTempFiles
			'WScript.Quit
		Else
			boolFound = True
		End If
		
		If boolFound = True Then DeleteFilesAtPath strTempFiles
	Next
 
	For Each objSubFolder In objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\").SubFolders
		boolFound = False
		strTempFiles = objSubFolder.Path & "\Local Settings\Temp"
		If objFSO.FolderExists(strTempFiles) = False Then
			'MsgBox "Could not find " & strTempFiles
			WriteToLog "Could not find " & strTempFiles
			'WScript.Quit
		Else
			boolFound = True
		End If
		
		If boolFound = True Then DeleteFilesAtPath strTempFiles
	Next
 
	boolFound = False
	strTempFiles = objShell.ExpandEnvironmentStrings("%WINDIR%") & "\Temp"
	If objFSO.FolderExists(strTempFiles) = False Then
		'MsgBox "Could not find " & strTempFiles
		WriteToLog "Could not find " & strTempFiles
		'WScript.Quit
	Else
		boolFound = True
	End If
	
	If boolFound = True Then DeleteFilesAtPath strTempFiles
 
End Sub
'======== END OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
 
Sub DeleteFilesAtPath(strThePath)
	strTempFiles = objFSO.GetFolder(strThePath).ShortPath
 
'======== METHOD 1 ===================
	strCacheLog = "Cache Files Cleared on " & objShell.ExpandEnvironmentStrings("%COMPUTERNAME%") & ": " & Now & VbCrLf & "========================================"
	For Each objFile In objFSO.GetFolder(strTempFiles).Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next	
	For Each objSubFolder In objFSO.GetFolder(strTempFiles).SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
	WriteToLog strCacheLog
'======== METHOD 2 ==================
	On Error Resume Next
	objFSO.DeleteFile(strTempFiles & "\*.*")
	Err.Clear
	On Error GoTo 0
	WriteToLog "Files deleted from " & strTempFiles
End Sub
 
Sub RecurseSubFolders(objFolder)
	For Each objFile In objFolder.Files
		On Error Resume Next
		strFilePath = objFile.Path
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next
 
	For Each objSubFolder In objFolder.SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
End Sub
'=======================
 
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function
 
Sub WriteToLog(strText)
	Set objCacheLog = objFSO.OpenTextFile(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ClearCacheLog.txt", 8, True)
	objCacheLog.Write VbCrLf & VbCrLf & strText
	objCacheLog.Close
	Set objCacheLog = Nothing
End Sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 21828390
Rob i get this

---------------------------
Windows Script Host
---------------------------
Script:      \\sophos\c$\Delete Temp Latest.vbs
Line:      60
Char:      4
Error:      Variable is undefined: 'strRemoteUser'
Code:      800A01F4
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828391
Rob i get this

---------------------------
Windows Script Host
---------------------------
Script:      \\sophos\c$\Delete Temp Latest.vbs
Line:      60
Char:      4
Error:      Variable is undefined: 'strRemoteUser'
Code:      800A01F4
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21828415
Please change this line:
                  WriteToLog " *** *** Starting Script against " & strRemoteUser & " on " & strComputer & ": " & Now

to this
                  WriteToLog " *** *** Starting Script against all users on " & strComputer & ": " & Now


Regards,


Rob.
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828435
Rob i get these messages in the  file

 *** *** Starting Script against all users on dev-mrd01: 6/20/2008 9:50:01 AM
Does that mean its clear.
Can we have the data to a file on what has been deleted and where
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828436
Rob i get these messages in the  file

 *** *** Starting Script against all users on dev-mrd01: 6/20/2008 9:50:01 AM
Does that mean its clear.
Can we have the data to a file on what has been deleted and where
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21828454
Hmmm, that shouldn't be the end of the log.....it should tell you if files were deleted, or the folders were not found....

I might have to test it....

Rob.
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828464
Does this happen in the "ClearCacheLog.txt"?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21828661
Yes, should do.....
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828703
But its not population in that file...
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828704
But its not population in that file...
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828768
Rob i downloaded the same psexec file and now it works for some machines i get this...

---------------------------
Windows Script Host
---------------------------
Script:      \\sophos\c$\Delete Temp Latest.vbs
Line:      47
Char:      4
Error:      Permission denied: 'GetObject'
Code:      800A0046
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828769
Rob i downloaded the same psexec file and now it works for some machines i get this...

---------------------------
Windows Script Host
---------------------------
Script:      \\sophos\c$\Delete Temp Latest.vbs
Line:      47
Char:      4
Error:      Permission denied: 'GetObject'
Code:      800A0046
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828779
Does the script delete within this path too for all profiles?

C:\Documents and Settings\username\Local Settings\Temp

I checked but the files are still there within the Temp folder
0
 
LVL 11

Author Comment

by:bsharath
ID: 21828780
Does the script delete within this path too for all profiles?

C:\Documents and Settings\username\Local Settings\Temp

I checked but the files are still there within the Temp folder
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 21828946
It deletes
Local Settings\Temp
Local Settings\Temporary Internet Files
Windows\Temp

and also now removes empty folders.

Just change the path to PSExec and it should work.

Regards,

Rob.
' Delete_Temp_Files_Remotely
Option Explicit
 
Dim objShell, objNetwork, objProcess, strUserName, strHostName, strCommand, strAdminUser, strAdminPass, strComputer
Dim objWMIService, colComputer, objComputer, boolClearCache, objFSO, strTempFiles
Dim strCacheLog, objInputFile, objFile, strFilePath, objSubFolder, objCacheLog, boolFound
Const intForReading = 1
 
If Left(WScript.ScriptFullName, 2) <> "\\" Then
	MsgBox "Please run this script from a UNC path."
	WScript.Quit
End If
 
Set objShell = CreateObject("Wscript.Shell")
Set objProcess = objShell.Environment("Process")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strUserName = objProcess("USERNAME")
strHostName = objProcess("COMPUTERNAME")
 
If WScript.Arguments.Count < 1 Then
	Call Normal_User_Commands
ElseIf WScript.Arguments(0) = "AsAdmin" Then
	Call Admin_User_Commands
Else
	MsgBox "Unknown Argument received"
End If
 
Sub Normal_User_Commands
	' Enter your Administrative credentials here
	strAdminUser = InputBox("Enter user name:", "User name", "DEVELOPMENT\Administrator")
	strAdminPass = InputBox("Enter the password for " & strAdminUser & ":", "Password")
	
    'MsgBox "Running as initiating user"
    'strComputer = "."
    Set objInputFile = objFSO.OpenTextFile("Computers.txt", intForReading, False)
    While Not objInputFile.AtEndOfStream
	    '***********************  THIS MSGBOX CAN BE REMOVED FOR A LOGON SCRIPT *******************
	    ' If this MsgBox is used, the admin commands can be run on a remote machine.
	    'strComputer = InputBox("Enter computer to clear Temporary Internet Files on:", "Enter Computer", "172.16.2.64")
	    '******************************************************************************************
		strComputer = objInputFile.ReadLine
		If Ping(strComputer) = False Then
			'MsgBox strComputer & " did not respond to ping. Cannot continue script."
			WriteToLog strComputer & " did not respond to ping. Cannot continue script."
		Else
		
			On Error Resume Next
			Set objWMIService = GetObject("winmgmts:" _
			    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
			If Err.Number = 0 Then
				Err.Clear
				On Error GoTo 0
			    ' This command assumes that PSExec.exe (available from Microsoft) is stored on the shared folder
			    ' below.  It runs PSExec as the Admin user, on a remote machine, or the current machine if it is a
			    ' logon script.  It re-runs this file again, this time passing the "AsAdmin" argument, so the script
			    ' knows it has been run under an Admin account, and can safely execute the Admin_User_Commands Sub.
			    ' NOTE: If the Admin is running this script from a mapped drive, the client must have the same drive mapped,
			    ' because of the use of WScript.ScriptFullName below.  It is best to run this script from a UNC path,
			    ' so the client machine can access the file via a UNC path and not rely on a mapped drive.
				WriteToLog " *** *** Starting Script against all users on " & strComputer & ": " & Now
				strCommand = "cmd /c \\mccdc01\netlogon\psexec_194.exe -accepteula -i -e -u " & strAdminUser & " -p " & strAdminPass & " \\" & strComputer & " WScript """ & WScript.ScriptFullName & """ AsAdmin"
				objShell.Run strCommand, 1, True
			    ' NOTE: In the above two lines, you can change the cmd /c to cmd /k and the strCommand, 0, True to strCommand, 1, True
			    ' if you wish to see some output for debugging / testing purposes.
			    MsgBox "Finished running on " & strComputer
			Else
				Err.Clear
				On Error GoTo 0
				MsgBox "Error connecting to " & strComputer
				WriteToLog "Error connecting to " & strComputer
			End If
		End If
	Wend
 
End Sub
 
Sub Admin_User_Commands
	' Now the script has detected that "AsAdmin" was passed to it, and will run these commands.
	' Now running as Administrator on the target macchine
	' MsgBox "Running as Admin"
 
	For Each objSubFolder In objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\").SubFolders
		boolFound = False
		strTempFiles = objSubFolder.Path & "\Local Settings\Temporary Internet Files"
		If objFSO.FolderExists(strTempFiles) = False Then
			'MsgBox "Could not find " & strTempFiles
			WriteToLog "Could not find " & strTempFiles
			'WScript.Quit
		Else
			boolFound = True
		End If
		
		If boolFound = True Then DeleteFilesAtPath strTempFiles
	Next
 
	For Each objSubFolder In objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") & "\Documents and Settings\").SubFolders
		boolFound = False
		strTempFiles = objSubFolder.Path & "\Local Settings\Temp"
		If objFSO.FolderExists(strTempFiles) = False Then
			'MsgBox "Could not find " & strTempFiles
			WriteToLog "Could not find " & strTempFiles
			'WScript.Quit
		Else
			boolFound = True
		End If
		
		If boolFound = True Then DeleteFilesAtPath strTempFiles
	Next
 
	boolFound = False
	strTempFiles = objShell.ExpandEnvironmentStrings("%WINDIR%") & "\Temp"
	If objFSO.FolderExists(strTempFiles) = False Then
		'MsgBox "Could not find " & strTempFiles
		WriteToLog "Could not find " & strTempFiles
		'WScript.Quit
	Else
		boolFound = True
	End If
	
	If boolFound = True Then DeleteFilesAtPath strTempFiles
 
End Sub
'======== END OF CLEAR TEMPORARY INTERNET FILES CACHE BLOCK ===========
 
Sub DeleteFilesAtPath(strThePath)
	strTempFiles = objFSO.GetFolder(strThePath).ShortPath
 
'======== METHOD 1 ===================
	strCacheLog = "Cache Files Cleared on " & objShell.ExpandEnvironmentStrings("%COMPUTERNAME%") & ": " & Now & VbCrLf & "========================================"
	For Each objFile In objFSO.GetFolder(strTempFiles).Files
		strFilePath = objFile.Path
		On Error Resume Next
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next	
	For Each objSubFolder In objFSO.GetFolder(strTempFiles).SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
	WriteToLog strCacheLog
'======== METHOD 2 ==================
	On Error Resume Next
	objFSO.DeleteFile(strTempFiles & "\*.*")
	Err.Clear
	On Error GoTo 0
	WriteToLog "Files deleted from " & strTempFiles
	
	RemoveEmptyFolders strTempFiles
End Sub
 
Sub RemoveEmptyFolders(strFolder)
	strCommand = "cmd /c dir " & strFolder & " /ad/b/s | sort /r > C:\EMPTYFOLDERS.TXT"
	objShell.Run strCommand, 0, True
	strCommand = "cmd /c for /f ""tokens=*"" %i in (C:\EMPTYFOLDERS.TXT) do rd /q %i"
	objShell.Run strCommand, 0, True
	objFSO.DeleteFile "C:\EMPTYFOLDERS.TXT", True
	WriteToLog "Empty folders removed from from " & strFolder
End Sub
 
Sub RecurseSubFolders(objFolder)
	For Each objFile In objFolder.Files
		On Error Resume Next
		strFilePath = objFile.Path
		objFSO.DeleteFile strFilePath, True
		If Err.Number = 0 Then
			strCacheLog = strCacheLog & VbCrLf & "SUCCESS: " & strFilePath
		Else
			Err.Clear
			strCacheLog = strCacheLog & VbCrLf & "FAILURE: " & strFilePath
		End If
		On Error GoTo 0
	Next
 
	For Each objSubFolder In objFolder.SubFolders
		Call RecurseSubFolders(objSubFolder)
	Next
End Sub
'=======================
 
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function
 
Sub WriteToLog(strText)
	Set objCacheLog = objFSO.OpenTextFile(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ClearCacheLog.txt", 8, True)
	objCacheLog.Write VbCrLf & VbCrLf & strText
	objCacheLog.Close
	Set objCacheLog = Nothing
End Sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 21829168
Thanks Rob worked perfect...
0
 
LVL 11

Author Comment

by:bsharath
ID: 21829169
Thanks Rob worked perfect...
0
 
LVL 11

Author Comment

by:bsharath
ID: 21852204
0
 
LVL 11

Author Comment

by:bsharath
ID: 21862335
Rob any help on the above question and these...

http://www.experts-exchange.com/Programming/Languages/Q_23488913.html
http://www.experts-exchange.com/Microsoft/Applications/Q_23504284.html
Already experts have commented on all 3 but need your help in getting them to final stage....
0
 

Expert Comment

by:murryc
ID: 22369127
Rob, I love this script you have created.  I am needing some changes and have posted it here.
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23696630.html
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question