Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting Registry KEY with Subkeys

260 views
Skip to first unread message

John LaCount

unread,
Apr 2, 2001, 1:49:34 PM4/2/01
to
Is there a way to delete a registry key without first deleting its
subkeys?
I have not found any documentation on this, but what I am after is
similar to the folderdelete function with the force switch.

If there is not a way to do this is there a way to step through registry
keys like steping through directories?

Thanks for the help in advance....

- John

Michael Harris

unread,
Apr 2, 2001, 2:23:43 PM4/2/01
to
Deleting registry keys...

On a Win9x system, RegDelete can delete a key with subkeys, but on NT4 or Win2K, it can't. The
reason is that the underlying Win32 registry API used by RegDelete is different on Win9x vs
non-Win9x.

There are other registry components that can delete keys with subkeys on non-Win9x. Two in
particular are RegObj.dll (a free MS component) and WMI 1.5's StdRegProv (WMI is available as a
separate MS upgrade for non-Win2K platforms, both Win9x and NT)...

Actually WMI doesn't supports it directly, but does support emunerating subkeys so you can write
your own recursive delete routine. RegObj on the other hand supports the recursive delete
automatically.

One simple way is to write a temp.reg file that you "execute" via WshShell.Run...

For example, assuming a key of "HKEY_CURRENT_USER\MyKey" with a named value "\\foo\bar"

Use FileSystemObject methods to write a temp.reg file containing

REGEDIT4
[HKEY_CURRENT_USER\MyKey]
"\\\\foo\\bar"=-

Note that each \ in the value name must be "escaped" with another \ , and be sure to use WriteLine
so that each line has a CR+LF pair at the end (including the last line)...

"\\\\foo\\bar"=- means delete the named value \\foo\bar.

Just for future reference,

[-HKEY_CURRENT_USER\MyKey] means delete the entire key.

Then "execute" the temp.reg with

set shell = createobject("wscript.shell")
'...run regedit in "silent" mode...
shell.run "regedit /s temp.reg"""

and then delete the temp.reg file.

I don't know if any of the key and named value delete tricks with .reg files is documented - I just
picked these up from other newsgroup posts myself...

--
Michael Harris
Microsoft.MVP.Scripting
--
mik...@mvps.org
Please do not email questions - post them to the newsgroup instead.
--

"John LaCount" <john.l...@kc.frb.org> wrote in message news:3AC8BBAE...@kc.frb.org...

Michael Harris

unread,
Apr 2, 2001, 8:47:56 PM4/2/01
to
Here's a WMI example of deleting a registry key even if it has subkeys...

StdRegProv
http://msdn.microsoft.com/library/psdk/wmisdk/regprovref_6yie.htm

set objRegistry = _
GetObject("winmgmts:root/default:StdRegProv")

const HKEY_CLASSES_ROOT = &H80000000
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
const HKEY_USERS = &H80000003
const HKEY_CURRENT_CONFIG = &H80000005
const HKEY_DYN_DATA = &H80000006

Dim rc
Dim sPath

sPath = "MyKey"

DeleteRegistryKey HKEY_CURRENT_USER, sPath

msgbox "done..."

Sub DeleteRegistryKey(hive,key)

rc = objRegistry.EnumKey(hive,key,arSubKeys)

for each sKey in arSubKeys
DeleteRegistryKey hive, key & "\" & sKey
next

msgbox Key
rc = objRegistry.DeleteKey(hive,key)
msgbox Key & " rc=" & rc

end sub

--
Michael Harris
Microsoft.MVP.Scripting
--
mik...@mvps.org
Please do not email questions - post them to the newsgroup instead.
--

"Michael Harris" <Please....@To.NewsGroup> wrote in message news:OA#xPI6uAHA.1796@tkmsftngp02...

Alexander Voit

unread,
Apr 10, 2001, 8:49:49 AM4/10/01
to
Afaik you cannot directly delete a whole key which still includes subkeys,
but I wrote a function for this task:

function Reg_DeleteKey(RegTree, Key)
result = DeleteAllKeys(RegTree, Key)
if result = 0 then
Reg_DeleteKey = 0
else
Reg_DeleteKey = "Error " & result
end if
end function
' --------------------------------------------------
Function DeleteAllKeys(mainkey, thiskey)
set wmireg = GetObject("winmgmts:root\Default:StdRegProv")
result = wmireg.EnumKey(mainkey, thiskey, subkeys)
If Not result = 0 Then
Exit function
End If

For each subkey in subkeys
result = DeleteAllKeys(mainkey, thiskey & "\" & subkey)
if result <> 0 then
DeleteAllKeys = result
exit function
end if
Next
DeleteAllKeys = DeleteKey(mainkey, thiskey)
End Function
'----------------------------------------------------

It's not tested very well but maybe you give it a try.

Alex Voit

0 new messages