Trouble using RegRead to see if RegKey exists 
Author Message
 Trouble using RegRead to see if RegKey exists

I need to find if the regkey entry below exists. I need to verify using only
HKCU\Software\ACME\.
I am not able to read that successfully though. Can you help ?
The problem I need to solve will not have a szValue available for me.

Entire RegKey:
HKCU
   Software
      ACME   (<--- I want to identify the key using this point)
         FortunateTeller      szMindReader

I am doing:

Set WshShell = WScript.CreateObject("WScript.Shell")
'I just want to find out if HKCU\Software\ACME exists:

bKey = WshShell.RegRead("HKCU\Software\ACME\")
WScript.Echo WshShell.RegRead("HKCU\Software\ACME") '***** Getting error
here
If Err.Number <> 0 Then
    'If was I able to read the key successfully, I could say that Err.Number
<> 0 means key doesn't exist
   MsgBox "Key \ACME doesn't exist !"
Else
   MsgBox "Key \ACME exists."
End If



Fri, 01 Jul 2005 13:09:47 GMT  
 Trouble using RegRead to see if RegKey exists

Quote:

> I need to find if the regkey entry below exists. I need to verify using only
> HKCU\Software\ACME\.
> I am not able to read that successfully though. Can you help ?

Hi


Subject: Re: Check if registry key exists
Newsgroups: microsoft.public.scripting.VBScript, microsoft.public.scripting.wsh
Date: 2002-09-19 14:32:04 PST
http://groups.google.com/groups?selm=3D8A41AD.44D95564%40hydro.com

In the link  above, you will find two functions using WMI, one to check for
registry key and another one checking if a value exists. See further down in the
post for a functions that only uses WSH RegRead to check if a key exists. Note
that to use WSHs RegRead method to do this correctly, you need to test on the
error message text and not the error number,  and this error message may differ
in non-English versions of WSH ! The WMI method does not suffer under this
limitation.

More about the RegRead issue:

That is not a sufficient test to see if the key exist or not by simply look at
the error number when trying to read a key. You also need to test on the error
message because there is two cases where an error will be thrown, but one of
them will be when the key actually exists (and you need to test on the error
message because the error number will be same, only the error message differ).

If the key DO exists, but no value data exist for the default value for the key
(very common) the script above will give this error message:

   Unable to open registry key "HKCU\Software\ACME\" for reading.

This is the error message that will be given if the key does not exist:

   Invalid root in registry key "HKCU\Software\ACME\"

So, you can e.g. test that the 7 leftmost characters of e.message is the value
"Invalid" to detect a missing key. Also note that this error message can be in
an non-English language if you have installed a localized language version of
WSH and the test for the string "Invalid" may fail. The WMI solution I have
linked to in my other post does not suffer by this "shortcoming".

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and a ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter



Fri, 01 Jul 2005 19:04:33 GMT  
 Trouble using RegRead to see if RegKey exists
You guys are incredible !!



Quote:

> > I need to find if the regkey entry below exists. I need to verify using
only
> > HKCU\Software\ACME\.
> > I am not able to read that successfully though. Can you help ?

> Hi


> Subject: Re: Check if registry key exists
> Newsgroups: microsoft.public.scripting.vbscript,

microsoft.public.scripting.wsh
Quote:
> Date: 2002-09-19 14:32:04 PST
> http://groups.google.com/groups?selm=3D8A41AD.44D95564%40hydro.com

> In the link  above, you will find two functions using WMI, one to check
for
> registry key and another one checking if a value exists. See further down
in the
> post for a functions that only uses WSH RegRead to check if a key exists.
Note
> that to use WSHs RegRead method to do this correctly, you need to test on
the
> error message text and not the error number,  and this error message may
differ
> in non-English versions of WSH ! The WMI method does not suffer under this
> limitation.

> More about the RegRead issue:

> That is not a sufficient test to see if the key exist or not by simply
look at
> the error number when trying to read a key. You also need to test on the
error
> message because there is two cases where an error will be thrown, but one
of
> them will be when the key actually exists (and you need to test on the
error
> message because the error number will be same, only the error message
differ).

> If the key DO exists, but no value data exist for the default value for
the key
> (very common) the script above will give this error message:

>    Unable to open registry key "HKCU\Software\ACME\" for reading.

> This is the error message that will be given if the key does not exist:

>    Invalid root in registry key "HKCU\Software\ACME\"

> So, you can e.g. test that the 7 leftmost characters of e.message is the
value
> "Invalid" to detect a missing key. Also note that this error message can
be in
> an non-English language if you have installed a localized language version
of
> WSH and the test for the string "Invalid" may fail. The WMI solution I
have
> linked to in my other post does not suffer by this "shortcoming".

> --
> torgeir
> Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and a ONLINE version of the 1328 page
> Scripting Guide: http://www.microsoft.com/technet/scriptcenter



Fri, 01 Jul 2005 23:29:50 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. help please: example of seeing of a registry key exists

2. help please: example of seeing of a registry key exists

3. RegRead If Exists

4. RegRead throws an exception when the variable doesn't exist

5. RegRead:. Key doesn't exist

6. seeing if a file exists on a hard drive

7. Seeing if file exists. Please Help

8. Help!...Trouble overwritting existing file

9. Read RegKey

10. RegKey Hlep (Please)

11. Enum RegKey

12. Using RegRead to get version of Office Applications

 

 
Powered by phpBB® Forum Software