From: David on 12 Apr 2010 14:52 I have a vbs script that updates a registry key. Sometimes the key does not exist. I would like to test the existence of the key before doing the regwrite or trap the exception when the regwirte fails and contiune with the script. my experience with exceptions is limited and would appreciate any guidence. I first tried a simple "if" statement but got a "inalid root in registry" ------- If (objWshShell.RegRead ("HKCU\Software\Microsoft\Office\9.0\Word\Options\DOC-PATH") = True) Then Wscript.Echo "Its there" Else Wscript.Echo "Its not there" End If thanks, David
From: Richard Mueller [MVP] on 12 Apr 2010 15:35 "David" <David(a)discussions.microsoft.com> wrote in message news:39E09E30-AFBD-4FAB-BF0D-A5D7A36F66EB(a)microsoft.com... >I have a vbs script that updates a registry key. Sometimes the key does not > exist. > I would like to test the existence of the key before doing the regwrite or > trap the exception when the regwirte fails and contiune with the script. > my > experience with exceptions is limited and would appreciate any guidence. > > I first tried a simple "if" statement but got a "inalid root in registry" > ------- > If (objWshShell.RegRead > ("HKCU\Software\Microsoft\Office\9.0\Word\Options\DOC-PATH") = True) Then > Wscript.Echo "Its there" > Else > Wscript.Echo "Its not there" > End If > > > thanks, > David You need to attempt to read the registry value, then trap the possible error. I've used a function similar to below: ========= Dim objShell, strKey Set objShell = CreateObject("Wscript.Shell") strKey = "HKLM\Software\MyApp\Version" If (KeyExists(strKey) = True) Then Wscript.Echo "Registry entry exists" Else Wscript.Echo "Registry entry does NOT exist" End If Function KeyExists(ByVal strKey) ' Function to check if registry setting exists. ' Object reference objShell must have global scope. Dim strValue ' Trap error if strKey does not exist. On Error Resume Next strValue = objShell.RegRead(strKey) If (Err.Number = 0) Then KeyExists = True Else KeyExists = False End If End Function -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net --
From: LikeToCode on 12 Apr 2010 15:55 Will this work? I don't know the value of the DOC-PATH key so I made one up. If it returns null then the value is wrong or the key does not exist. Set objShell = CreateObject("wscript.shell") Const HKEY_CURRENT_USER = &H80000001 Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv") strKeyPath = "Software\Policies\Microsoft\Office\9.0\Word\Options" strValueName = "DOC-PATH" strValue = "C:\Program Files" objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue If IsNull(strValue) Then MsgBox "Exists" Else MsgBox "Does Not Exist." End If
From: Mayayana on 12 Apr 2010 21:46 Here's a 3rd version: Function Exists(RegPath) Dim r On Error Resume Next Err.clear r = SH.RegRead(RegPath) If hex(Err.number) = "80070002" Then Exists = False Else Exists = True End If End Function I've had some trouble on Win7 with WScript.Shell. I haven't heard of anyone else having trouble, and I haven't tested it exhaustively, but there was one instance where a simple operation failed. I had permission. It was 32-bit. But it failed. The same operation worked fine with WMI. As a result I decided to write a WMI Reg. class, using StdRegProv. WMI Registry functions are poorly designed and WMI itself is slow and bloated. But putting it all into a class works fairly well. The class is here if you're curious: www.jsware.net/jsware/scripts.php5#wmirclas It wraps the WMI mess and just exposes clear, simple functions. The nice thing about WMI is that you can do key/value enumeration and binary values. The class functions, which all return informative error codes, are as follows. Exists CreateKey(Path) EnumKeys(Path, ArrayOut) EnumVals(Path, AValsOut, ATypesOut) GetValue(Path, Type) SetValue(Path, Val, Type) Delete(Path) ------------------- |I have a vbs script that updates a registry key. Sometimes the key does not | exist. | I would like to test the existence of the key before doing the regwrite or | trap the exception when the regwirte fails and contiune with the script. my | experience with exceptions is limited and would appreciate any guidence. | | I first tried a simple "if" statement but got a "inalid root in registry" | ------- | If (objWshShell.RegRead | ("HKCU\Software\Microsoft\Office\9.0\Word\Options\DOC-PATH") = True) Then | Wscript.Echo "Its there" | Else | Wscript.Echo "Its not there" | End If | | | thanks, | David
|
Pages: 1 Prev: long name for logon script user Next: Taste of Powershell |