Re: Reading vs. Checking Registry Key
- From: "scott" <sbailey@xxxxxxxxxxxxxxx>
- Date: Tue, 12 Apr 2005 09:13:01 -0500
Can you help me with the usage of the function? In "MY CODE" below, I do a
simple test for sKey1. When I run it, it always returns "exists", even if I
change the skey1 variable to a non-existant key.
MY CODE
***************
Dim WshShell, bKey, sKey, sValueBad, sValueGood
Set WshShell = WScript.CreateObject("WScript.Shell")
sKey1 = "HKLM\Software\Webroot\Enterprise\CommAgent\Su"
If KeyExists(sKey1) = True Then
WScript.Echo "exists"
Else
WScript.Echo "Key does not exist"
End If
Function KeyExists(sKeyPath)
keyExists= false: if (sKeyPath="") then exit function
on error resume next
createobject("wscript.shell").regRead sKeyPath
select case err
case 0: keyExists= true
case &h80070002: dim sErrMsg
sErrMsg= replace(err.description, sKeyPath, "")
err.clear
createobject("wscript.shell").regRead "HKEY_ERROR\"
keyExists= not (sErrMsg=replace(err.description, _
"HKEY_ERROR\", ""))
case else: keyExists= false
end select
on error goto 0
End function
"Joe Earnest" <jearnest3-SPAM@xxxxxxxxxxxxx> wrote in message
news:%23MFJmtvPFHA.3668@xxxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> "scott" <sbailey@xxxxxxxxxxxxxxx> wrote in message
> news:OwsGkGvPFHA.2748@xxxxxxxxxxxxxxxxxxxxxxx
>>I can read the below key value fine, but I need to be able to check and
>>see
>>if the key exists before reading it to avoid script errors.
>>
>> To my knowledge, there are only 3 methods available in the WSH Library to
>> access registry values: regread, regwrite and regdelete.
>>
>> Anyone have a trick to testing if a key exists before taking action on
>> it?
>>
>> SCRIPT
>> =========
>> WshShell.RegRead("HKLM\Software\Webroot\Enterprise\CommAgent\TheKey")
>
> If you don't want to use WMI, here's my version of a trick that Torgeir
> Bakken taught me, that he attributes to Walter Zachary. It's an
> error-trapped attempt to read a key's default value, distinguishing
> between
> the error description for no default value assigned and key not found,
> which
> have the same error number, but different descriptions. The way it's set
> up, it's applicable to any language version. The function below doesn't
> test syntax and expects the key path to have a final backslash, in order
> for
> the test to be valid.
>
> ---
> function KeyExists (sKeyPath)
> keyExists= false: if (sKeyPath="") then exit function
>
> on error resume next
> createobject("wscript.shell").regRead sKeyPath
>
> select case err
> case 0: keyExists= true
>
> case &h80070002: dim sErrMsg
> sErrMsg= replace(err.description, sKeyPath, "")
> err.clear
>
> createobject("wscript.shell").regRead "HKEY_ERROR\"
> keyExists= not (sErrMsg=replace(err.description, _
> "HKEY_ERROR\", ""))
>
> case else: keyExists= false
> end select
> on error goto 0
> end function
> ---
>
> Joe Earnest
>
>
>
.
- Follow-Ups:
- Re: Reading vs. Checking Registry Key
- From: Joe Earnest
- Re: Reading vs. Checking Registry Key
- References:
- Reading vs. Checking Registry Key
- From: scott
- Re: Reading vs. Checking Registry Key
- From: Joe Earnest
- Reading vs. Checking Registry Key
- Prev by Date: Re: Help! This is too much for me....
- Next by Date: Re: Reading vs. Checking Registry Key
- Previous by thread: Re: Reading vs. Checking Registry Key
- Next by thread: Re: Reading vs. Checking Registry Key
- Index(es):
Relevant Pages
|