Script to delete temp files on xp machines

Posted on 2003-11-19
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.  
Question by:oregonmutual
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
LVL 24

Expert Comment

ID: 9781392
LVL 14

Accepted Solution

aelatik earned 125 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")

Expert Comment

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

Expert Comment

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.

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

If strComputer = "" Then
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 & """")

'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."


'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
           WScript.Sleep 1000
           Set objMyProcess = Service.Get("Win32_Process='" & intProcessID & "'")
           If Err.Number <> 0 Then
           ' Process has finished or terminated
              bolDone = True
              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
         GetRegKeyStrValue = "Failed"
      End If
      Set oRegistry = Nothing
End Function



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

EE Zone Advisor


Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…

751 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