Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Delete Folders using WMI

Reply
Thread Tools Display Modes

Delete Folders using WMI

 
 
Babu VT
Guest
Posts: n/a

 
      06-29-2009
Hi,
I'm trying to clean "$NTUninstall..." folders from remote machines and found
the below snippet useful.Unfortunately it isn't deleting any folders.Can
someone point me where is the fault... I can't use FSO objects since I need
to delete folders in multiple machines where C$ shares might not be
available.

strComputer ="."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strQueryString1 = "Select * from Win32_Directory where Name = '" &
OSRootDir & "'"
Set colSubfolders = objWMIService.ExecQuery (strQueryString1)
For Each objFolder in colSubfolders
If Instr( UCase(objFolder.Name), "$NTUNINSTALL" ) > 0 then
strReport = strReport & "Deleted :" & strcomputer & ":" & objFolder.Name
& " uninstall folder" & vbCrLf
objFolder.delete
End If

Next


 
Reply With Quote
 
 
 
 
Pegasus [MVP]
Guest
Posts: n/a

 
      06-29-2009

"Babu VT" <> wrote in message
news:uFeetFN%...
> Hi,
> I'm trying to clean "$NTUninstall..." folders from remote machines and
> found the below snippet useful.Unfortunately it isn't deleting any
> folders.Can someone point me where is the fault... I can't use FSO objects
> since I need to delete folders in multiple machines where C$ shares might
> not be available.
>
> strComputer ="."
> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
> strQueryString1 = "Select * from Win32_Directory where Name = '" &
> OSRootDir & "'"
> Set colSubfolders = objWMIService.ExecQuery (strQueryString1)
> For Each objFolder in colSubfolders
> If Instr( UCase(objFolder.Name), "$NTUNINSTALL" ) > 0 then
> strReport = strReport & "Deleted :" & strcomputer & ":" & objFolder.Name
> & " uninstall folder" & vbCrLf
> objFolder.delete
> End If
>
> Next


You need to assign a value to OSRootDir, e.g. "c:\\Windows". Alternatively
this Scripting Guy item might help:
How Can I Delete a Folder and All Its Subfolders?
http://www.microsoft.com/technet/scr...5/hey0405.mspx


 
Reply With Quote
 
Mark D. MacLachlan
Guest
Posts: n/a

 
      07-01-2009
Babu VT wrote:

> Hi,
> I'm trying to clean "$NTUninstall..." folders from remote machines
> and found the below snippet useful.Unfortunately it isn't deleting
> any folders.Can someone point me where is the fault... I can't use
> FSO objects since I need to delete folders in multiple machines where
> C$ shares might not be available.
>
> strComputer ="."
> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
> "\root\cimv2") strQueryString1 = "Select * from Win32_Directory where
> Name = '" & OSRootDir & "'" Set colSubfolders =
> objWMIService.ExecQuery (strQueryString1) For Each objFolder in
> colSubfolders If Instr( UCase(objFolder.Name), "$NTUNINSTALL" ) > 0
> then strReport = strReport & "Deleted :" & strcomputer & ":" &
> objFolder.Name & " uninstall folder" & vbCrLf objFolder.delete
> End If
>
> Next


Rather than just deleting the uninstalls, I prefer to check the age of
the folders just to be sure it is safe to remove them. That is why I
wrote the following which will only delete the folders if they are more
than 2 weeks old.

Code:
'=======================================================================
===
'
' NAME: CleanHotfixUninstalls.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL   : http://www.thespidersparlor.com
' COPYRIGHT (c) 2008 All rights reserved
' DATE  : 05/07/2008
'
' COMMENT:
'
' This script will enumerate Uninstall folders under the Windows
directory
' and delete them if they are more than two weeks old
'
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE
SUPPLIERS
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'=====================================
Dim fso, WshShell
Dim oFolder, oSubFolder

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell=CreateObject("WScript.Shell")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

Path = WinDir

Set oFolder = fso.GetFolder(Path)
Set colSubfolders = oFolder.Subfolders

For Each oSubfolder in colSubfolders
If Left(oSubFolder.Name,1) = "$" Then
If Right(oSubFolder.Name,1) = "$" Then
'If DateDiff("d", oSubFolder.DateCreated,Now) > 14 Then
fso.DeleteFolder(oSubFolder)
'End If
End If
End If
Next

Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing
--

 
Reply With Quote
 
Mark D. MacLachlan
Guest
Posts: n/a

 
      07-01-2009
Just realized the posted version had the two week check commented out.

Code:
'=======================================================================
===
'
' NAME: CleanHotfixUninstalls.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL   : http://www.thespidersparlor.com
' COPYRIGHT (c) 2008 All rights reserved
' DATE  : 05/07/2008
'
' COMMENT:
'
' This script will enumerate Uninstall folders under the Windows
directory
' and delete them if they are more than two weeks old
'
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE
SUPPLIERS
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'=====================================
Dim fso, WshShell
Dim oFolder, oSubFolder

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell=CreateObject("WScript.Shell")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

Path = WinDir

Set oFolder = fso.GetFolder(Path)
Set colSubfolders = oFolder.Subfolders

For Each oSubfolder in colSubfolders
If Left(oSubFolder.Name,1) = "$" Then
If Right(oSubFolder.Name,1) = "$" Then
If DateDiff("d", oSubFolder.DateCreated,Now) > 14 Then
fso.DeleteFolder(oSubFolder)
End If
End If
End If
Next

Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing
 
Reply With Quote
 
Mark D. MacLachlan
Guest
Posts: n/a

 
      07-02-2009
Sorry for the repost, but I noticed I had the date checks commented out
on the previously posted version.

Code:
'=======================================================================
===
'
' NAME: CleanHotfixUninstalls.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL   : http://www.thespidersparlor.com
' COPYRIGHT (c) 2008 All rights reserved
' DATE  : 05/07/2008
'
' COMMENT:
'
' This script will enumerate Uninstall folders under the Windows
directory
' and delete them if they are more than two weeks old
'
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE
SUPPLIERS
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'=====================================
Dim fso, WshShell
Dim oFolder, oSubFolder

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell=CreateObject("WScript.Shell")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

Path = WinDir

Set oFolder = fso.GetFolder(Path)
Set colSubfolders = oFolder.Subfolders

For Each oSubfolder in colSubfolders
If Left(oSubFolder.Name,1) = "$" Then
If Right(oSubFolder.Name,1) = "$" Then
If DateDiff("d", oSubFolder.DateCreated,Now) > 14 Then
fso.DeleteFolder(oSubFolder)
End If
End If
End If
Next

Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to delete user files and folders that won't delete? boe Windows Server 5 04-21-2008 10:36 PM
Re: Script to delete all files and folders except some specific folders Pegasus \(MVP\) Scripting 0 02-27-2008 10:07 PM
Re: Delete all files and folders except some specific folders Pegasus \(MVP\) Windows Server 0 02-27-2008 10:01 PM
Can I Delete These Folders? Brandon Windows Vista General Discussion 1 03-13-2007 01:36 PM
Special permissions - Delete and Delete Files and Folders Paul Dunn Windows Server 0 05-03-2006 03:51 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59