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