Solved

Script to delete temp files on xp machines

Posted on 2003-11-19
4
6,927 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 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")
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now