Hi,

I need to safely check is a registry key exists, and if not, create it:

aKey = HKLM/Software/MyApp/aPath

psuedo code:

if Exists aPath then
s = aPath
else
Create reg key aPath := "C:\MyApp"
end if

What would this be in vbs..?

rgds

jake

Re: Safely check for reg key existence.. by Pegasus

Pegasus
Mon Apr 28 16:39:59 CDT 2008


"Jake" <jake56@gmail.com> wrote in message
news:O5PpadWqIHA.3428@TK2MSFTNGP02.phx.gbl...
> Hi,
>
> I need to safely check is a registry key exists, and if not, create it:
>
> aKey = HKLM/Software/MyApp/aPath
>
> psuedo code:
>
> if Exists aPath then
> s = aPath
> else
> Create reg key aPath := "C:\MyApp"
> end if
>
> What would this be in vbs..?
>
> rgds
>
> jake

Here is the basic idea. You will need to customise it to match
your requirements, e.g. with respect to hive, key, subkey and
subsubkey and also with respect to the type of registry
data (Reg_SZ, Reg_DWord etc). You might find it easier to
perform the job with regedit.exe - see here:
http://support.microsoft.com/?kbid=310516

01. Const sComputer = "."
02. Const sKeyPath = "SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Winlogon"
03. Const sValueName = "TestValue"
04. Const iNewValue = 10
05. Const HKEY_LOCAL_MACHINE = &H80000002 'HKLM hive
06.
07. Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
08. sComputer & "\root\default:StdRegProv")
09.
10. oReg.GetDWordValue HKEY_LOCAL_MACHINE, sKeyPath, sValueName, iCurrValue
11.
12. If IsNull(iCurrValue) Then
13. WScript.Echo "The value " & sValueName & " does not exist"
14. oReg.CreateKey HKEY_LOCAL_MACHINE, sKeyPath, sValueName, iCurrValue
15. oReg.SetDWordValue HKEY_LOCAL_MACHINE, sKeyPath, sValueName, iNewValue
16. Else
17. WScript.Echo "Data for " & sValueName & " is " & iCurrValue
18. End If



Re: Safely check for reg key existence.. by mayayana

mayayana
Mon Apr 28 18:08:47 CDT 2008

'-- tests for key or value.
Public Function Exists(RegPath)
Dim r, SH
On Error Resume Next
Err.clear
Set SH = CreateObject("WScript.Shell")
r = SH.RegRead(RegPath)
If Hex(Err.number) = "80070002" Then
Exists = False
Else
Exists = True
End If
Set SH = Nothing
End Function

You can also do it with WMI, as Pegasus pointed
out, but it's a pain in the neck. WMI adds functions
that are missing in the WScript.Shell Registry functions,
like the ability to enumerate keys and values. But it's
a clunky design that *requires* enumerating just to
find out what sort of value you've got and is more work
in general than the equivalent WScript.Shell functions.

If it's of any value to you, you can get a Registry class
here:

www.jsware.net/jsware/scripts.php3#classpk

The Registry class in the download is where the above
Exists function came from.

I actually wrote a WMI StdRegProv wrapper class
recently, including an Exists function that uses
enumeration to check for an existing key or value.
It then returns the data type in the case of a value,
if it exists. But if you don't have to use StdRegProv
it's much easier not to. I haven't really used my new
class to speak of yet. I cringe hearing the disk
thrashing that tells me WMI must be loading an
awfully lot of bloated libraries just to check a Registry
value. :)

>
> I need to safely check is a registry key exists, and if not, create it:
>
> aKey = HKLM/Software/MyApp/aPath
>
> psuedo code:
>
> if Exists aPath then
> s = aPath
> else
> Create reg key aPath := "C:\MyApp"
> end if
>
> What would this be in vbs..?
>
> rgds
>
> jake



Re: Safely check for reg key existence.. by mayayana

mayayana
Mon Apr 28 19:03:47 CDT 2008


Woops. I missed the second part of your question. It's not
clear exactly what you're doing. aPath is a value in a key MyApp?
And you want to read it if it exists, or write it otherwise?
(You don't need to test for existence if you don't need
to read it. You can just write the key and value in that case.
It won't harm anything if the value already exists and gets
written again.)

This might be what you need for what you seem to want:

Sim SH, AppPath
Set SH = CreateObject("WScript.Shell")
On Error Resume Next
AppPath = SH.RegRead("HKLM\Software\MyApp\aPath")
If Hex(Err.number) = "80070002" Then
SH.RegWrite "HKLM\Software\MyApp\aPath", "C:\MyApp"
End if
Set SH = Nothing

That assumes you want to have a value named
aPath in a key named MyApp. If you actually want
aPath to be a key, with the path string as default
value, you'd want it like this, with backslashes in the
reg. path:

AppPath = SH.RegRead("HKLM\Software\MyApp\aPath\")
If Hex(Err.number) = "80070002" Then
SH.RegWrite "HKLM\Software\MyApp\aPath\", "C:\MyApp"
End if

I suppose that in this case you could also just check the
read return rather than do an Exists check:

AppPath = SH.RegRead("HKLM\Software\MyApp\aPath")
If Len(AppPath) = 0 Then '-- not found, so write the value.
SH.RegWrite "HKLM\Software\MyApp\aPath", "C:\MyApp"
End if