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

a script to delete a registry key 4

Status
Not open for further replies.

tech888

MIS
Sep 3, 2003
26
0
0
CA
I get the error:

"unable to remove registry key HKEY_LOCAL_MACHINE\SOFTWARE\cal1"

This key has nothing in it, just empty. I created cal1 to test the script.


code below
-----------------------------------------------

Option Explicit
Dim WshShell, CAL1

Set WshShell = WScript.CreateObject("WScript.Shell")
CAL1 = "HKEY_LOCAL_MACHINE\SOFTWARE\cal1"
WshShell.RegDelete cal1

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

what am I doing wrong?
 
Have you tried this ?
Code:
Set WshShell = WScript.CreateObject("WScript.Shell")
CAL1 = "HKEY_LOCAL_MACHINE\SOFTWARE\cal1\"
WshShell.RegDelete cal1

Hope This Help
PH.
 
yes, i have tried this.

I'm pretty sure I'm getting the error because there is a default string inside the key. Windows will not let you delete a key with anything inside of it.

Gonna have to find another way to delete the string inside. then delete the key.

 
In my box, with the trailing \, all the key hierarchy is deleted....

Hope This Help
PH.
 
Phv,

what do you mean by in my box?
so your suggesting i should try
WshShell.RegDelete cal1\ (with the trailing backslash)

I am at home now. I will try it at work tomorrow.

thanks
 
I suggest this:
Code:
CAL1 = "HKEY_LOCAL_MACHINE\SOFTWARE\cal1\"
'                                       ^
or this:
Code:
WshShell.RegDelete cal1 & "\"

Hope This Help
PH.
 

CAL1 = "HKEY_LOCAL_MACHINE\SOFTWARE\cal1\" did not work for me

But
WshShell.RegDelete cal1 & "\" did!

Thanks a bunch!
 
But, adding a backslash here or there is exactly the same, tech888. So you must be making mistake somewhere. (We should not promote myth, should we?) cal1, ca11, CAL1, CA11 are sometimes hard to read from the screen.

- tsuji
 
tsuji,

well here is the working code. I just tested it again to make sure. (notice "cal1" is all lowercase to avoid any confusion)

The registry key in regedit.exe reads

My Computer\HKEY_CURRENT_CONFIG\cal1


working
-------------------------------------------------------
Set WshShell = WScript.CreateObject("WScript.Shell")
cal1 = "HKEY_CURRENT_CONFIG\cal1\"
WshShell.RegDelete cal1 & "\"
-------------------------------------------------------
This works fine and dandy.

But..
taking out the & "\" will give me an error
Line:3
Char:1
Error: Unable to remove registry key "HKEY_CURRENT_CONFIG\cal1\".
Code: 80070002
Source WshShell.RegDelete


However I added another key within the cal1.
ie. HKEY_CURRENT_CONFIG\cal1\cal2
the working code above does not work anymore..
It was ok with a string inside but not with the new key.
The error I got for running the same working code above is.

Line:3
Char:1
Error: Unable to remove registry key H&quot;KEY_CURRENT_CONFIG\cal1\\&quot;. <--notice the \Code: 80070005
Source WshShell.RegDelete

I am only using those 3 lines in the code. Do I need to put anything else in?

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

What I'm trying to do is clear a key in the registry that contains all the printers on a server. the server key contains printer subkeys. I just need to delete the server key and everything in it. Didn't know it would be this complicated.

I thought i had it PHV!

I'm glad I'm not a full time programmer =)
 
tech888,

First a couple of preliminary notes

[1] When you test the change on the registry with regedit opened up on a separate window to monitor the effect, you need to press F5 key to update the registry window. (In case you forget to update and come to conclusion.)

[2] Vbs may well be very tolerant on seeing a key with double, triple backslash at the end, but the basic is &quot;one backslash&quot; suffice.

Now, I just ask you to highlight copy and paste these lines to a vbs and test:
Code:
Dim WshShell, cal1
Set WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;)
cal1 = &quot;HKEY_CURRENT_CONFIG\cal1\&quot;
WshShell.RegDelete cal1
Does it delete the subkey &quot;cal1&quot; under HKEY_CURRENT_CONFIG ?
Yes or no? With due respect, I am not asking you to repeat the script you have already tested. I just think there is something you've missed out. But, first test it and post back.

regards - tsuji

 
tsuji,

Yes your code did work. I guess leaving out the first line
Dim WshShell, cal1 results in the error. I guess that a bisic declaration line you must write. =)

But the confusing thing is the working code from my last posting did not require the first line &quot;Dim WshShell, cal1&quot;. the code executed and remove cal1 fine. The only difference was the & &quot;\&quot;


the question now is how do you del the cal1 key if there are other keys inside of it?

thanks for your clarification!

 
tech888,

Thanks for the feedback.

That is exactly the point I had in mind without mentioning it because it is not very eloquent putting in words.

Even without Option Explicit, scripters are not free to do whatever with variables. The WshShell as declared here is a very good example. You can spare dim cal1, but no, you cannot spare dim WshShell. Helas! I see nowhere in the documentation though. That is my own little observation and had learnt it the hard way.

Your mentioning that without dim WshShell, double &quot;\&quot; works---this I still have to verify. I take you words. If true, it is yet another abbreration! (When I posted the first message, I had no idea you meant trailing &quot;\\&quot;. I thought you mean adding &quot;\&quot; on the string cal1 or on the regdelete line.)

As to the followup question, I think it does not require the key being empty. It will just delete the whole sub-tree.

regards - tsuji
 
Hello tech888,

Just to do my part on testing and feedback on the pending issues that I am interested in verifying.

[1] Put this part to an end. Dim WshShell is needed even without Option Explicit. This I knew and again am happy to refresh my experience on this.

[2] Double or multiple trailing &quot;\&quot;. (Pre-requisite dim wshshell, without it this is a non-issue because one cannot get pass the createobject line. Hence, I cannot confirm your experience in this sense.) I am satisfied that double or even mulitple trailing backslash is compiled to the same effect as single backslash. No compilation or runtime error would raise. Hence, it is built in the parsing routine at compilation time. It is a bit distorted/morbid, but it is good to know. A use of it is that blindly appending a trailing backslash if one's in mind for a variable be a key and spare the validation whether a key has already a backslash there. Not encouraging in using this, but I accept it as a feature.

Thank you for a couple of your followup postings.

regards - tsuji
 
I've discovered that the
Code:
 RegDelete
method with a key argument (trailing \) have different behavior, depending on the windows platform:
On Win9x (95,98,ME) all the key hierarchy is deleted.
On WinNT (NT,2k,XP) if any subkey exists you raise error 80070005.

Hope This Help
PH.
 
Hello PHV,

Quite right. The abberration is attributed to the different realization of registry api along the line of win9x and winNT respectively. Good point, PHV.

regards - tsuji
 
I have attempted to delete a registry branch without success and have come to the same conclusions you've stated in your last note .... regdelete can delete the branch structure on non-NT OS, but the key must be empty for regdelete to work correctly on the NT. I have found documentation describing how to delete the registry branch structure if using the .Net environment, however I am still chained to VB6.

My question is ... has anyone been able to delete a registry branch (not empty) using VB6 (or script) on the NT platform?
 
At this point I am looking at a recursive enumeration of the regitry keys, then RegDelteKey using the API ... probably less time to do that than it has taken me to look for alternative solutions.
 
Try this to remove all subkeys too:

Code:
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\MenuOrder\Favorites"

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top