Re: Reading vs. Checking Registry Key



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



.


Loading