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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting subkeys from the registry

Status
Not open for further replies.

josephk73

Technical User
Aug 1, 2002
115
GB
Hi Guys,

I'm having problems deleting a registry key through a script. Its not a permission issue because I can delete the key if I physically go onto the box.

I'm using the standard shell object and implementing the RegDelete method. So we have something like this

objshell=CreateObject("Wscript.Shell")
objshell.RegDelete <key in question>

The error I get is "Unable to remove registry key <reg key in question>. I dont know if this helps, but the registry key in question is under HKCU

I ran some tests and I'm quite ceartain the problem has to do with 'child' keys under the 'parent' key. For example, if I I create a dummy key, with no 'child' keys everything works fine. If I then create 'child' keys to the 'parent' key things stop working.

Has anybody experienced this behaviour. O BTW, I get the same problem if I use WMI to run the query.

Cheers in advance
 
>I ran some tests and I'm quite ceartain the problem has to do with 'child' keys under the 'parent' key.
For winnt series, you've to recursively delete all subkeys and values before finally delete the parent key.
 
tsuji,

Thanks for that. youre right about the problem being with the child keys. I've got a sub routine that recursively deletes the regkeys starting from the bottom upto the top and this works fine. If anybody is interested in the code, just shout and I'll post it.
 
josephk73,

In my opinion, ALL learning is good... if only for the sake of learning.

Recursive deletion within the Windows Registry can be - and has been - a problem for many people, myself included.

I personally would be very grateful if you would please post the code you used. I'm also sure that many others would also appreciate the info. (It's one of the many reasons why this forum is such a great resource!)

In other words... SHOUT! <grin>

Regards,

Rick
 
Rick998 wrote:
>Recursive deletion within the Windows Registry can be - and has been - a problem for many people, myself included.

The recursive deletion should be based on the functionality of recursive enumeratione the subkeys and valuename and valuetype based on a primary or base key. Deletion is an accident. One might desire displaying the value itself, other might desire modifying specific value. Here is my preferred clean approach of enumeration with specific care in _not_ to mixing scope of variables that I see happening all the times. Variable passing by reference for some types of variables in vbs is understood and made good use of.
Code:
const hkcr=&h80000000
const hkcu=&h80000001
const hklm=&h80000002

dim nbranch,pkey,snode,oreg,i
dim akey(),avalue()

nbranch=hklm
pkey="Hardware"
snode="."

redim akey(0) : akey(0)=pkey
'avalue(0,.) valuename, avalue(1,.) value type per wmi spec
redim avalue(1,-1)

set oreg=getobject("winmgmts:{impersonationLevel=impersonate}!\\" & snode & "\root\default:StdRegProv")
enumkey nbranch,pkey,akey,oreg
for i=0 to ubound(akey)
    enumvalues nbranch,akey(i),avalue,oreg
next
set oreg=nothing

'just for displaying & verifying
for i=0 to ubound(akey)
    wscript.echo i & vbcrlf & akey(i)
next
for i=0 to ubound(avalue,2)
    wscript.echo avalue(0,i) & vbcrlf & avalue(1,i)
next

sub enumkey(nbr,sbase,akey, oreg)
    dim asubkey,i,n,m
    oreg.enumkey nbr,sbase,asubkey
    n=ubound(akey) : m=ubound(asubkey) 
    redim preserve akey(n+m+1)
    for i=0 to ubound(asubkey)
        akey(n+i+1)=sbase & "\" & asubkey(i)
    next
    for i=0 to ubound(asubkey)
        enumkey nbr,sbase & "\" & asubkey(i),akey,oreg
    next
end sub

sub enumvalues(nbr,sbase,avalue,oreg)
    dim avn,avt,i,n,m
    oreg.enumvalues nbr,sbase,avn,avt
    n=ubound(avalue,2) : m=ubound(avn)

    redim preserve avalue(1,n+m+1)
	
    for i=0 to ubound(avn)
        avalue(0,n+i+1)=sbase & "\" & avn(i)
        avalue(1,n+i+1)=avt(i)
    next
end sub
The resultant output are key names array and a 2-dim array storing the value name and value type per wmi spec. That is enough for deletinge/modifing value. If delete the parent key is desired, after deleting all values, delete keys are done in reverse order of the key names array. And it'll be done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top