FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


need help from VBS guru.... how to delete a registry key?





jdelfire
I don't know if im the appropriate forum here but please correct me if i'm wrong. btw im a noob in VBS. here is what the deal i need to delete a registry key and all the subkey and value inside it using a VB script. i successfully deleted a key using this VBscript.
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\"

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\Microsoft\Windows\CurrentVersion\Policies"

lRC = DeleteRegEntry(HKEY_CURRENT_USER, 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(HKEY_CURRENT_USER, sEnumPath, sNames)

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

On Error Goto 0

' 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


but the problem is i also want to delete a registry key under "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\Browser Helper Objects" i tried applying the same scripts above by replacing the target but somehow its not working. VBS guru need your help. Thanx in advance
MrBlueSky
I think you have to change the following 2 lines:

Code:

lRC = DeleteRegEntry(HKEY_CURRENT_USER, sPath)


and

Code:

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


to

Code:

lRC = DeleteRegEntry(HKEY_LOCAL_MACHINE, sPath)


and

Code:

lRC = objRegistry.EnumKey(HKEY_LOCAL_MACHINE, sEnumPath, sNames)
jdelfire
Thanks for the reply man. i tried changing the scripts just like what you instructed - no erros though - but the registry is still intact.
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\Browser Helper Objects\
i just notice one thing i can delete any registry key under HKLM but not on HKCU. is there any special work around for that?
maybe you guys have some scripts of your own that i can copy?


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\Microsoft\Windows\CurrentVersion\explorer\Browser Helper Objects\"

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(HKEY_LOCAL_MACHINE, sEnumPath, sNames)

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

On Error Goto 0

' 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
MrBlueSky
You can also have a look at this topic: http://www.frihost.com/forums/vt-94124.html&highlight=
Maybe the script over there will work for you.
jdelfire
still not working mr. blue ive tried both in xp and vista. the 1st script works fine if under HKCU. any other ways? i'm going crazy ^_^ thanx again dude
jdelfire
Option Explicit
On Error Resume Next
Dim p1, p2, p3, p4 , p5 , p6 , p7 , WshShell, MyBox, vbdefaultbutton

p1 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\"
p2 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\system\"
p3 = "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\system\"
p4 = "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\explorer\"
p5 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\explorer\"
p6 = "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\"
p7 = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\Browser Helper Objects\"
Set WshShell = WScript.CreateObject("WScript.Shell")


WshShell.RegDelete p1
WshShell.RegDelete p2
WshShell.RegDelete p3
WshShell.RegDelete p4
WshShell.RegDelete p5
WshShell.RegDelete p6
WshShell.RegDelete p7

Set WshShell = Nothing

MyBox = MsgBox("Succesfully delete registry entries.", 64, "Yeah")
Related topics
Windows Tips&tricks!
751 Useful Windows XP Files
Windows Vista - Official Thread
CD Drives disapperd
Transferring Applications
Shutdown time....
HELP with installing 300 GB HDD
Help
Windows XP SP3 comming soon
SHORTCUT KEY
Virus in Computer
8 Ghostly registry deletes every time I boot my Portable
cant open the regedit...
Malware removed, but problems linger.
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.