Hi,

I am trying to read a reg key from the registry. the problem is that if the
key doesn't exist, the script jumps to "If Err.Number <> 0 Then", or cause
runtime error, if i don't use "On Error Resume Next"
I wanted to use it in the following way:

Set RegistryOBJ = WScript.CreateObject("WScript.Shell")
'returns the path of my app writen in the registry
Get_ETS_PathInRegistry = RegistryOBJ.RegRead(ETS_RegistryLocation)
If ETS_UnInstallPath = False Then
'if the key doesn't exist, we can install the app
Install_ETS(ETS_InstallPath)
Else
'if the key exists, we should uninstall it and then install the new
version(might be in new path)
UnInstall_ETS(ETS_UnInstallPath)
'ETS_InstallPath is being obtained from user.
Install_ETS(ETS_InstallPath)
End If

The problem is that it doesn;t go to the "If ETS_UnInstallPath = False Then"
but as i described above, go to the end of the script and then ends the
script.
in this way, i will need to add in "If Err.Number <> 0 Then"
"Install_ETS(ETS_InstallPath)
" command.
Isn't there an elegant way to do so, or i should consider doing it using VC?

Re: RegRead by Torgeir

Torgeir
Wed Nov 03 02:32:56 CST 2004

Ronen wrote:

> Hi,
>
> I am trying to read a reg key from the registry. the problem is that if
> the key doesn't exist, the script jumps to "If Err.Number <> 0 Then",
> or cause runtime error, if i don't use "On Error Resume Next"
> I wanted to use it in the following way:
>
> [snip]
Hi

I assume you are talking about checking/reading a registry value
and not a key here, as it looks like it can return a path if app
is installed.

In that case, I suggest you use the RegRead function I use, below
is a rewritten version of your script (I also have changed the
word "key" to "value" in the code comments).

If you in some other case needs to check if a key or value exists
you can use the RegKeyExists and RegValueExists functions in the
VBScript in this link:
http://groups.google.com/groups?selm=%23t94ENtLEHA.2532%40TK2MSFTNGP10.phx.gbl


'--------------------8<----------------------
'returns the path of my app writen in the registry
ETS_UnInstallPath = RegRead(ETS_RegistryLocation)

If ETS_UnInstallPath = "" Then
'if the value doesn't exist, we can install the app
Install_ETS(ETS_InstallPath)
Else
'if the value exists, we should uninstall it and then
' install the new version(might be in new path)
UnInstall_ETS(ETS_UnInstallPath)
'ETS_InstallPath is being obtained from user.
Install_ETS(ETS_InstallPath)
End If

Function RegRead(ByVal sRegValue)
Set oShell = CreateObject("WScript.Shell")
On Error Resume Next
RegRead = oShell.RegRead(sRegValue)
' If the value does not exist, error is raised
If Err Then
RegRead = ""
Err.clear
End If
' If a value is present but uninitialized the RegRead method
' returns the input value in Win2k.
If VarType(RegRead) < vbArray Then
If RegRead = sRegValue Then
RegRead = ""
End If
End If
On Error Goto 0
End Function
'--------------------8<----------------------




--
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/default.mspx