Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Re: a script to delete a registry key

Status
Not open for further replies.

tntteam

Technical User
Mar 15, 2010
1
FR
thread329-674375

Sorry to open new topic and reference an old topic that is 6 years old, but it helped me a lot and I fixed the original solution :

Problem : unable to delete a registry key if it has subvalues on windows xp (error code wsh 80070002)

utilman offered a script solution but it has a little bug, so here is the fixed one :

--------------------------------------------------------------

Code:
On Error Resume Next

Const HKEY_CLASSES_ROOT  = &H80000000
Const HKEY_CURRENT_USER  = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS         = &H80000003

' Object used to get StdRegProv Namespace
Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")

' Object used to determine local machine name
Set wshNetwork = CreateObject("WScript.Network")

' Registry Provider (StdRegProv) lives in root\default namespace.
Set wmiNameSpace = wmiLocator.ConnectServer(wshNetwork.ComputerName, "root\default")
Set objRegistry = wmiNameSpace.Get("StdRegProv")

' Deletes Key with alle subkeys
sPath = "Software\Policies\Microsoft\Windows\System\Scripts\Startup\"

lRC = DeleteRegEntry(HKEY_LOCAL_MACHINE, sPath)

Function DeleteRegEntry(sHive, sEnumPath)
' Attempt to delete key.  If it fails, start the subkey
' enumration process.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)

' The deletion failed, start deleting subkeys.
If (lRC <> 0) Then

' Subkey Enumerator
   On Error Resume Next

   lRC = objRegistry.EnumKey(sHive, sEnumPath, sNames)

   For Each sKeyName In sNames
      If Err.Number <> 0 Then Exit For
      lRC = DeleteRegEntry(sHive, sEnumPath & "\" & sKeyName)
   Next

' At this point we should have looped through all subkeys, trying
' to delete the registry key again.
   lRC = objRegistry.DeleteKey(sHive, sEnumPath)

End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top