Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script to delete temp files on xp machines

Posted on 2003-11-19
4
Medium Priority
?
6,959 Views
Last Modified: 2012-06-22
I'm trying to write a vbs or wsh script to delete temp files and temp internet files on xp machines.  Deleting these files and folders isnt a problem, but how do I get down to the c:\documents and settings\username\local settings\temp folder to delete these files.  
0
Comment
Question by:oregonmutual
4 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9781392
0
 
LVL 14

Accepted Solution

by:
aelatik earned 500 total points
ID: 9781455
You can get the desktop and from there you can change desktop to "local settings/temp".

Set WshShell = Wscript.CreateObject("WScript.Shell")
Wscript.Echo Replace(WshShell.SpecialFolders("Desktop"), "Desktop", "local settings\temp")
0
 
LVL 3

Expert Comment

by:ToolMan
ID: 9781470
Hi there,

about your Q:
i myself use the following to cleanup tempfiles -->

REM ---------BOF-----------
@echo off
echo Cleaning up tmp files..........
md c:\robocopy
robocopy c:\robocopy "c:\Temp" /mir /r:1 /w:1
robocopy c:\robocopy "%userprofile%\Local Settings\Temp" /mir /r:1 /w:1
robocopy c:\robocopy "%temp%" /mir /r:1 /w:1
robocopy c:\robocopy "%tmp%" /mir /r:1 /w:1

robocopy c:\robocopy "%userprofile%\Local Settings\Temporary Internet Files" /mir /r:1 /w:1
robocopy c:\robocopy "%userprofile%\Local Settings\History" /mir /r:1 /w:1
robocopy c:\robocopy "%userprofile%\Application Data\Microsoft\Office\Recent" /mir /r:1 /w:1
robocopy c:\robocopy "%userprofile%\recent" /mir /r:1 /w:1
robocopy c:\robocopy "%userprofile%\Cookies" /mir /r:1 /w:1
robocopy c:\robocopy "%userprofile%\NetHood" /mir /r:1 /w:1
robocopy c:\robocopy %SystemRoot%\Temp  /mir /r:1 /w:1
robocopy c:\robocopy %SystemRoot%\Minidump  /mir /r:1 /w:1
robocopy c:\robocopy "%ALLUSERSPROFILE%\Documents\DrWatson"  /mir /r:1 /w:1
robocopy c:\robocopy "%userprofile%\Local Settings\Application Data\Microsoft\Terminal Server Client\Cache"  /mir /r:1 /w:1
rd c:\robocopy
REM ---------EOF-----------

ti's a DOS batchfiles and works great for me, however it does use the ROBOCOPY.EXE copy resoursekit utility to remove the files.

hope it helps
Regards
0
 

Expert Comment

by:ronnac
ID: 14709176
Here is a vbs script that I have successfully used for a couple of years on XP desktops. It will ask you for the name of the remote PC and display the deletion progress in an IE window. It goes through all windows profiles on that remote PC.

'---------BOF-----------
strComputer = Inputbox ("PC, on which you want to delete temp files:")

If strComputer = "" Then
      Wscript.Quit
End IF

Set Locator = CreateObject("WbemScripting.SwbemLocator")
Set Service = Locator.ConnectServer(strComputer, "root/cimv2")
Set Security = Service.Security_
Security.ImpersonationLevel = 3



'open message screen
'*******************

Set objExplorer = WScript.CreateObject ("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 800
objExplorer.Height = 300
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Visible = 1
objExplorer.Document.Write ("<head><title>" & strComputer & " Temp Files</title></head>")
objExplorer.Document.Write ("Deleting temp files and folders on " & strComputer & "<br>")


'Looping users folders
'*********************

objExplorer.Document.Write ("Deleting Temp folders<br>")

Set filesys = CreateObject("Scripting.FileSystemObject")

Set folder = filesys.GetFolder("\\" & strComputer & "\c$\documents and settings")  
Set folcoll = folder.SubFolders  
For Each subfol in folcoll
      TempFolder = subfol & "\Local Settings\Temp"
      exec_wait("cmd /c rmdir /s /q """ & TempFolder & """")
      exec_wait("cmd /c mkdir """ & TempFolder & """")
      objExplorer.Document.Write ("Deleting " & TempFolder & "<br>")
      TempFolder = subfol & "\Local Settings\Temporary Internet Files"
      objExplorer.Document.Write ("Deleting " & TempFolder & "<br>")
      exec_wait("cmd /c rmdir /s /q """ & TempFolder & """")
      exec_wait("cmd /c mkdir """ & TempFolder & """")
Next



'executing other deletions
'*************************

TempFolder = "\\" & strComputer & "\c$\WINDOWS\TEMP"
objExplorer.Document.Write ("Deleting " & TempFolder & "<br>")
exec_wait("cmd /c rmdir /s /q """ & TempFolder & """")

'recreate c:\windows\temp if necessary
If NOT filesys.FolderExists(TempFolder) Then
      filesys.CreateFolder TempFolder
End IF

If filesys.FolderExists(TempFolder) Then
      TempFolder = "\\" & strComputer & "\c$\WINDOWS\SoftwareDistribution\download"
      If filesys.FolderExists(TempFolder) Then
            objExplorer.Document.Write ("Deleting " & TempFolder & "<br>")
            exec_wait("cmd /c rmdir /s /q """ & TempFolder & """")
      End IF
End IF


objExplorer.Document.Write ("Deleting remaining *.tmp files<br>")

exec_wait("cmd /c c: && cd .. && cd .. && cd .. && del *.tmp *.~* ~*.* *.??~ *.--- *._mp *~tmp.* *.??$ /s /q")

objExplorer.Document.Write ("Done.<br>")
objExplorer.Document.title = "DONE: "& strComputer & " Temp Files."

'FUNCTIONS
'*********


'remotely execute and wait
'-------------------------

Function exec_wait(App)

      Set Proc = Service.Get("Win32_Process")

      errReturn = Proc.Create(App, Null, Null, intProcessID)

      ' errReturn will not give any install status, it only indicates
      ' if the app was successfully launched or not
      If errReturn = 0 Then
        ' wait for the process to finish
        On Error Resume Next
        Do
           WScript.Sleep 1000
           Set objMyProcess = Service.Get("Win32_Process='" & intProcessID & "'")
           If Err.Number <> 0 Then
           ' Process has finished or terminated
              bolDone = True
           Else
              bolDone = False
           End If
        Loop Until bolDone
        On Error Goto 0
      End If

End Function


'Get RegKey Function
Function GetRegKeyStrValue (sComputer, hTree, sKey, sString, sValue)
      Dim oRegistry
      Dim lResult

      Set oRegistry      = GetObject("winmgmts:{impersonationLevel=impersonate}//" & sComputer & "/root/default:StdRegProv")
      lResult = oRegistry.GetStringValue(hTree,   sKey,  sString ,sValue)
      If (lResult = 0) And (Err.Number = 0) Then
         GetRegKeyStrValue = sValue
      Else
         GetRegKeyStrValue = "Failed"
      End If
      Set oRegistry = Nothing
End Function

'---------EOF-----------

********************************************************************

NOTE!!!!!!!!!!!! Do not use this script on any xp or Server machine or you will destroy it - I only post here as i learnt the hard way on numerous machines and do not want anyone else to suffer what i have just finished dealing with

Jay_Jay70
EE Zone Advisor

*******************************************************************
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

783 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