> Hello all. What would be the best way to check to see if a registry subkey
> exists and if it does, query it for its permissions? Thanks in advance.
Hi
=================================================
Two functions using WMI, one to check for registry key and another one checking
if a value exists (se further down for a functions that only uses RegRead, no
WMI)
Const HKLM = &H80000002
Set oReg =GetObject("Winmgmts:root\default:StdRegProv")
sKeyPath = "Software\Microsoft\Windows\CurrentVersion"
sValue = "ProgramFilesDir"
If RegValueExists(sKeyPath, sValue) Then
WScript.Echo "Value exists"
Else
WScript.Echo "Value do not exist"
End If
If RegKeyExists(sKeyPath) Then
WScript.Echo "Key exists"
Else
WScript.Echo "Key do not exist"
End If
Function RegValueExists(sRegKey, sRegValue)
sRegKey = Trim(sRegKey)
sRegValue = LCase(Trim(sRegValue))
' init value
RegValueExists = False
If oReg.EnumValues(HKLM, sKeyPath, aValueNames, aValueTypes) = 0 Then
For i = 0 To UBound(aValueNames)
If LCase(aValueNames(i)) = sRegValue Then
RegValueExists = True
End If
Next
End If
End Function
Function RegKeyExists(sRegKey)
sRegKey = Trim(sRegKey)
If oReg.EnumValues(HKLM, sKeyPath, aValueNames, aValueTypes) = 0 Then
RegKeyExists = True
Else
RegKeyExists = False
End If
End Function
=================================================
When using WSH RegRead trying to read a registry key that does not exist, the
error description is:
Invalid root in registry key "HKEY_CURRENT_USER\Software\BlahBlah\".
In the case the key exists but have no default value it's:
Unable to open registry key "HKEY_CURRENT_USER\Software\" for reading.
!!! Note that this error message may differ in non-english versions of WSH !!!
If that could apply to you, you will need to add support for that as well in the
function below
Here is a function for you that tests on the err.description (it also adds a
trailing \ to the key path if it is missing):
Function RegKeyExists(sRegKey)
Set oShell = CreateObject("WScript.Shell")
RegKeyExists = True
sRegKey = Trim (sRegKey)
If Not Right(sRegKey, 1) = "\" Then
sRegKey = sRegKey & "\"
End if
On Error Resume Next
RegReadReturn = oShell.RegRead(sRegKey)
If Err Then
If LCase(Left(err.description,7)) = "invalid" Then
'key not found...
RegKeyExists = False
End if
Err.clear
End if
On Error Goto 0
End Function
--
torgeir