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

Checking that a registry key exists

Status
Not open for further replies.

josephk73

Technical User
Aug 1, 2002
115
GB
Hi All,

I'm using this bit of code to check if a registry value exists,

set wshShell= Wscript.CreateObject("WScript.Shell")
strKey= "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\"
present = WshShell.RegRead(strKey)
MsgBox strKey


But it keeps returning the message
"Unable to open registry key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\" for reading.

Now this is dumb, because I can see the key in the registry!!

So I tried another approach with this script.


set wshShell=Wscript.CreateObject("WScript.Shell")
strKey="HKLM\SOFTWARE\Microsoft\"

If RegKeyExists(strKey) = True Then
wscript.echo "key present"
Else
wscript.echo "key not present"
End If

Function RegKeyExists(strKey)
Dim strkeytotest
RegKeyExists = True
On Error Resume Next
strkeytotest = wshShell.RegRead(strKey)
If Err <> 0 Then
RegKeyExists = False
End If

End Function


However this dosen't work either, because it just keeps returning "Key not present", which again is dumb since the key for Microsoft is very much present.

I'm stuck and can't figure out why this ain't working. Any help would be greatly appreciated.

Cheers in advance.












 
josephk73,

It is a detail to the regread on key with default value not set. A large majority of key's default value is left undefined. Hence, I would say this is a very unwelcoming feature of wshshell.regread. This is a NT-series thing. I don't think win9x series will pose this problem.

To bypass this quirky feature, you have to test the err.description message to see if characteristic signature string exists. They have the same err.number!
[tt] if instr(1, err.description, signature, 1)<>0 then[/tt]
[1] If the test is positive based on the signature say "Unable to open registry key" then the key _is_ very well existing only the default value is not set.
[2] if the test find is positive based on the signature say "Invalid root in registry key" then the key is indeed inexistent.
But, a negative finding from [1] can safely deduce positive finding in [2]. So the refined testing can based on [1] alone.
(The test based on signature string in the err.description is highly localized, as anticipated.)

regards - tsuji
 
tsuji,

Thanks for taking the time to reply. This was not an easy one and I found the behaviour of the regread method really frustrating. The worst thing, is that so many books on scripting flippantly describe how regread should work for finding key names (and even have the cheek to provide coding examples - that dont work when you try them!!!), but forget to mention the problems. Very annoying.

Anycase, I have found a sure-fire way around the problem. I thought the problem was with my machine, but the same thing happened at work. As I've said it is sorted now, and your observations have been most helpful. Thanks again.
 
josephk73 , tsuji

I have been trying to do the same....I want to test for the presence of a registry key prior to running reg.exe to backup a chunk of the registry

I cant quite follow all of what has been said.

From josephk73

strkeytotest = wshShell.RegRead(strKey)

Have I got it right that I now check the err.decription.

I dont understand tsuji reply or the meaning of

if instr(1, err.description, signature,1) <> 0 then

Can you show the ammended solution?

Thank You

Si
 
bertieuk,

What I want to convey is this.
Code:
dim bExists
ssig="Unable to open registry key"

set wshShell= Wscript.CreateObject("WScript.Shell")
strKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\"
on error resume next
present = WshShell.RegRead(strKey)
if err.number<>0 then
    if right(strKey,1)="\" then    'strKey is a registry key
        if instr(1,strKey,ssig,1)<>0 then
            bExists=true
        else
            bExists=false
        end if
    else    'strKey is a registry valuename
        bExists=false
    end if
    err.clear
else
    bExists=true
end if
on error goto 0
if bExists=vbFalse then
    wscript.echo strKey & " does not exist."
else
    wscript.echo strKey & " exists."
end if
regards - tsuji
 
tsuji

thanks for the reply. when I run the example I always get "the key does not exist....it seems to me that

[ code ]
if instr(1,strKey,ssig,1)<>0 then
bExists=true
[ /code ]

is checking the error signature is contained withing the key path which I dont think it ever would and therefore always fail the check.

Si
I have tried to echo err.number but it seems to be uniniatialized.
 
bertieuk,

Sure, my bad. I wrote it up in realtime with too much distraction.! It is a slip of mind.
[tt]
'replace this line, _wrong_
'if instr(1,strKey,ssig,1)<>0 then
if instr(1,err.description,ssig,1)<>0 then
[/tt]
- tsuji
 
tsuji

Fantastic.....That has been a great help to the backup of my Imail configuration......Thanx

Si
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top