Re: Reading vs. Checking Registry Key
- From: "Joe Earnest" <jearnest3-SPAM@xxxxxxxxxxxxx>
- Date: Mon, 11 Apr 2005 19:03:10 -0600
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: scott
- Re: Reading vs. Checking Registry Key
- References:
- Reading vs. Checking Registry Key
- From: scott
- Reading vs. Checking Registry Key
- Prev by Date: Re: ExecMethodAsync ReturnValue
- Next by Date: Re: FolderExists Help.........
- Previous by thread: Reading vs. Checking Registry Key
- Next by thread: Re: Reading vs. Checking Registry Key
- Index(es):
Loading