Solved

Script to delete temp files on xp machines

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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

13 Experts available now in Live!

Get 1:1 Help Now