Re: CLean out all users Temp Dir Folder



Hello Daniel,

I've built the script you need with ScriptAhead a tool we have just
released that automatically creates scripts. Save the file as with a
wsf extension (in my example: TempFolderDelete.wsf). If you need any
modifications or a different script let me know.

Regards,
Thomas

========================================================
ScriptAhead - Windows Scripting without the hassle -
www.softomotive.com
========================================================


<?xml version="1.0"?>
<package>
<job>
<comment><![CDATA[
'*************************************************************************
' This script has been automatically generated by:
' ScriptAhead ver 1.0.0.368
'
' Script Name: TempFolderDelete.wsf
' Author: thomas
' Date: 28/6/2006
'*************************************************************************

]]></comment>
<script language="VBScript"><![CDATA[

On Error Resume Next


'******************************************************************************
'Task 1: Get Folder
' Get Folders that match the following criteria:
' Drive="c:" And Path="\documents and settings\"
' from the local computer and store them in $Folders
'******************************************************************************

ReDim v_Folders(-1)
Err.Clear
Set objWMI=GetSWbemServices(".","\root\cimv2", null)
If Err.Number<>0 Then ReportRuntimeError 1, "Error while trying to
connect to WMI Service in local computer"
Set colFolders=objWMI.ExecQuery("SELECT * FROM Win32_Directory" +
Replace(" WHERE Drive='c:' And Path='\documents and
settings\'","\","\\"),,48)
AppendCollectionToArray colFolders, v_Folders

'******************************************************************************
'Task 2: Loop through Collection
' Loop through Collection $Folders and at each iteration store the
' current item in $CurrentFolder
'******************************************************************************


For Each v_CurrentFolder In v_Folders

'******************************************************************************
'Task 3: Get File
' Get Files that match the following criteria:
' Drive="c:" And Path=$CurrentFolder.Path + $CurrentFolder.FileName +
' "\Local Settings\Temp\"
' from the local computer and store them in $Files
'******************************************************************************

ReDim v_Files(-1)
Err.Clear
Set objWMI=GetSWbemServices(".","\root\cimv2", null)
If Err.Number<>0 Then ReportRuntimeError 3, "Error while trying to
connect to WMI Service in local computer"
Set colFiles=objWMI.ExecQuery("SELECT * FROM CIM_DataFile" + Replace("
WHERE Drive='c:' And Path='" & v_CurrentFolder.Path &
v_CurrentFolder.FileName & "\Local Settings\Temp\'","\","\\"),,48)
AppendCollectionToArray colFiles, v_Files

'******************************************************************************
'Task 4: Delete File
' Delete Files $Files
'******************************************************************************


v_DeleteFileSucceeded=True
For Each objFile in v_Files
returnCode=objFile.Delete
v_DeleteFileSucceeded=v_DeleteFileSucceeded And (returnCode=0)

Next

'******************************************************************************
'Task 5: Get Folder
' Get Folders that match the following criteria:
' Drive="c:" And Path=$CurrentFolder.Path + $CurrentFolder.FileName +
' "\Local Settings\Temp\"
' from the local computer and store them in $TempSubfolders
'******************************************************************************

ReDim v_TempSubfolders(-1)
Err.Clear
Set objWMI=GetSWbemServices(".","\root\cimv2", null)
If Err.Number<>0 Then ReportRuntimeError 5, "Error while trying to
connect to WMI Service in local computer"
Set colFolders=objWMI.ExecQuery("SELECT * FROM Win32_Directory" +
Replace(" WHERE Drive='c:' And Path='" & v_CurrentFolder.Path &
v_CurrentFolder.FileName & "\Local Settings\Temp\'","\","\\"),,48)
AppendCollectionToArray colFolders, v_TempSubfolders

'******************************************************************************
'Task 6: Delete Folder
' Delete Folders $TempSubfolders
'******************************************************************************


v_DeleteFolderSucceeded=True
For Each objFolder in v_TempSubfolders
returnCode=DeleteDirectoryTree (objFolder, GetSWbemServices
(objFolder.CSName, "\root\cimv2", Null))
v_DeleteFolderSucceeded=v_DeleteFolderSucceeded And (returnCode=0)

Next

'******************************************************************************
'Task 7: If $DeleteFileSucceeded And $DeleteFolderSucceeded Then
'******************************************************************************

If v_DeleteFileSucceeded And v_DeleteFolderSucceeded Then

'******************************************************************************
'Task 8: Display Message
' Echo: "Folder " + $CurrentFolder.Path + $CurrentFolder.FileName +
' "\Local Settings\Temp\ cleared successfully"
'******************************************************************************

WScript.Echo "Folder " & v_CurrentFolder.Path &
v_CurrentFolder.FileName & "\Local Settings\Temp\ cleared successfully"

'******************************************************************************
'Task 9: Else
'******************************************************************************

Else

'******************************************************************************
'Task 10: Display Message
' Echo: "Some items in folder " + $CurrentFolder.Path +
' $CurrentFolder.FileName + "\Local Settings\Temp\ failed to be
' deleted"
'******************************************************************************

WScript.Echo "Some items in folder " & v_CurrentFolder.Path &
v_CurrentFolder.FileName & "\Local Settings\Temp\ failed to be deleted"

'******************************************************************************
'Task 11: End If
'******************************************************************************

End If

'******************************************************************************
'Task 12: End Loop
'******************************************************************************

Next


'*************************************************************************
' Helper Functions
'*************************************************************************

Function GetSWbemServices (strComputerName, strNamespace,
arrPrivileges)
Set objSWbemLocator=CreateObject ("WbemScripting.SWbemLocator")
Set objSWbemServices=objSWbemLocator.ConnectServer(strComputerName,
strNamespace)
objSWbemServices.Security_.ImpersonationLevel=3

If Not IsNull(arrPrivileges) Then
For Each strPrivilege In arrPrivileges
objSWbemServices.Security_.Privileges.AddAsString strPrivilege,True
Next
End If

Set GetSWbemServices=objSWbemServices
End Function



Sub AppendCollectionToArray (col, arr)
For Each item In col
ReDim Preserve arr(UBound(arr)+1)
Set arr(UBound(arr))=item
Next
End Sub



Function DeleteDirectoryTree (objFolder, objWMI)
Set colSubFolders=objWMI.ExecQuery("ASSOCIATORS OF
{Win32_Directory.Name='" & objFolder.Name & "'} Where AssocClass =
Win32_Subdirectory ResultRole = PartComponent")
For Each objSubFolder In colSubFolders
If DeleteDirectoryTree=0 Then DeleteDirectoryTree=DeleteDirectoryTree
(objSubFolder, objWMI)
Next
If DeleteDirectoryTree=0 Then DeleteDirectoryTree=objFolder.Delete
End Function



Sub ReportRuntimeError (taskNumber, errorMessage)
WScript.Echo "ScriptAheadRuntimeError Runtime error in task #" &
taskNumber & ": " & errorMessage & vbCrLf _
& "VBScript description for error: " & Err.Description
& " (" & Err.Number & ")"
WScript.Quit
End Sub


]]></script>
</job>
</package>



Daniel wrote:
Good morning,

I have been having trouble modifying a simple script to loop through all the
user folders on a server and clean out each user's temp direactory.

The basic script is:

*****
Dim arrFolders()
intSize = 0

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

strFolderName = "C:\Documents and Settings\Main\Local Settings\Temp"

Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1

For Each objFolder in colSubfolders
GetSubFolders strFolderName
Next

Sub GetSubFolders(strFolderName)
Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

For Each objFolder2 in colSubfolders2
strFolderName = objFolder2.Name
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
GetSubFolders strFolderName
Next
End Sub

For i = Ubound(arrFolders) to 0 Step -1
strFolder = arrFolders(i)
strFolder = Replace(strFolder, "\", "\\")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = '" & strFolder & "'")

For Each objFolder in colFolders
errResults = objFolder.Delete
Next
Next
wscript.echo "Done!"
*****

And work fine but I would like to be able to adapt it to loop through and
the user folders (excluding system folders) from the "C:\Documents and
Settings\" folder and clean out each of the temp folders associated to each
account.

I have tried adding and extra loop but have had no success. Any guidance is
appreciated.

Thank you.

Daniel

.



Relevant Pages

  • RE: Deleting 1 folder across multiple Mailboxes
    ... I too am looking to delete about 30 folders that came over from a linux ... are over 200 mailboxes that is a whole lot of click delete click delete. ... I found this script, but it seems to only work on Exchange 2000. ... Dim objCommand, objConnection, strBase, strFilter, strAttributes ...
    (microsoft.public.exchange.admin)
  • Re: Count Files and Folders
    ... I hacked the script a little to also include the total number of folders, ... Sub CountSubfolderFiles ... CountSubfolderFiles Subfolder ...
    (microsoft.public.scripting.vbscript)
  • Re: Archive Message Folder Script by Jolly Roger
    ... inside each of those root folders is a multitude of sub folders. ... Now I have run the script a number of times and it makes it through ...
    (microsoft.public.mac.office.entourage)
  • Re: Need help for a new scripter
    ... been tasked with comming up with a script that will allow me to move ... folders from the profile into it. ... Dim sUserProfile, sBUPath ... sPath & "\quicklaunch", OverWriteFiles ...
    (microsoft.public.scripting.vbscript)
  • Re: Path not found error using FSO GetFolder
    ... Sub ShowSubFolders ... For Each Subfolder in Folder.SubFolders 'Path Not Found error on this ... This is just a test script for part of another project where I need to list ... files in folders, however, I need to ignore folders that can't be accessed ...
    (microsoft.public.scripting.vbscript)