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

How to check if reg key exist in vbs?

32 views
Skip to first unread message

jarek

unread,
Sep 26, 2003, 4:55:13 PM9/26/03
to
I need to check a registry for a presence
of a certain key, before I actually
attempt to read its value.
Otherwise the script fails on the attempt
to read an inexisting key.
How can checking for the reg key's presence
be done within vb script?

Thanks,

JJ

p.s. I have to use VBH v.5.1 on the target machines.

Michael Harris (MVP)

unread,
Sep 26, 2003, 8:39:44 PM9/26/03
to
jarek wrote:
> I need to check a registry for a presence
> of a certain key, before I actually
> attempt to read its value.
> Otherwise the script fails on the attempt
> to read an inexisting key.
> How can checking for the reg key's presence
> be done within vb script?


There is no way to test for a key's existence other than to attempt the
RegRead with error trapping...

See:

Google Groups: View Thread "Reg key exists?"
http://groups.google.com/groups?th=b8e50dd9d330e4ee

>
> Thanks,
>
> JJ
>
> p.s. I have to use VBH v.5.1 on the target machines.

--
Michael Harris
Microsoft.MVP.Scripting

Windows 2000 Scripting Guide
Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp

System Administration Scripting Guide - samples scripts
http://www.microsoft.com/downloads/release.asp?ReleaseID=38942

WSH 5.6 documentation download
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en

jarek

unread,
Sep 27, 2003, 12:12:51 AM9/27/03
to

>There is no way to test for a key's existence other than
to attempt the
>RegRead with error trapping...

:) I've just worked it out the hard way.
I've thought of it after having no success
trying to find some kind of answer out there on the net.

I set On Error Resume Next
then run my RegRead
and next I check the value stored under Err.Number
if not equal 0.
This way I can make conditions forr all kinds of
returned error codes :)
Finally I reset the error code handler and proceed
with the script.

Thanks for your reply, though.
You just made me feel better about
the path of my thinking :)

JJ

Torgeir Bakken (MVP)

unread,
Sep 28, 2003, 7:29:37 PM9/28/03
to
jarek wrote:

> >There is no way to test for a key's existence other than
> >to attempt the RegRead with error trapping...
>
> :) I've just worked it out the hard way.
> I've thought of it after having no success
> trying to find some kind of answer out there on the net.
>
> I set On Error Resume Next
> then run my RegRead
> and next I check the value stored under Err.Number
> if not equal 0.

Hi

Testing on Err.Number when checking for the existence of a registry key is not
the correct solution I'm afraid, you will need to test on the Err.Description
part.

RegRead will always return this error description when trying to read a
non-existent key:
WshShell.RegRead: WshShell.RegRead: Invalid root in registry key
"HKEY_CURRENT_USER\Software\test\"

The "problem" is that if you try to test on a key that exists but have no
default value set, you will get this error:
Unable to open registry key "HKEY_CURRENT_USER\Software\test\" for reading.

and the error number is the *exact* same as the previous situation: -2147024894

That means that testing on a error number is no good, the *only* way to detect
a non-existing key is to test for the error description (or at least the first
part of it).

Because of the content of the Err.Description is different depending on the
language of WSH installed, this must also be taken into consideration, at least
in an international setting.

The following function will handle all the above issues:


If Not RegKeyExists("HKLM\SOFTWARE\Lotus\Notes\6.0") Then
WScript.Echo "Notes 6.0 is not installed"
End If


Function RegKeyExists(ByVal sRegKey)
' Returns True or False based on the existence of a registry key.

Dim sDescription, oShell
Set oShell = CreateObject("WScript.Shell")

RegKeyExists = True
sRegKey = Trim (sRegKey)
If Not Right(sRegKey, 1) = "\" Then
sRegKey = sRegKey & "\"
End If

On Error Resume Next
oShell.RegRead "HKEYNotAKey\"
sDescription = Replace(Err.Description, "HKEYNotAKey\", "")

Err.Clear
oShell.RegRead sRegKey
RegKeyExists = sDescription <> Replace(Err.Description, sRegKey, "")
On Error Goto 0
End Function

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


0 new messages