View Single Post
Old 07-02-2009   #9 (permalink)
mayayana


 
 

Re: Help with vbs script

A couple of other possibilities.... I haven't tried
these specifically on Win7, though:

If the check is local you can use WScript.Shell, which
is simpler than WMI:

Function Exists(RegPath)
Dim r
On Error Resume Next
r = SH.RegRead(RegPath) '--SH here is WScript.Shell object.
If Hex(Err.number) = "80070002" Then
Exists = False
Else
Exists = True
End If
End Function

If you have to use WMI it's awkward, but one way
is to call EnumKey on the parent key. If that succeeds
and the sought after key name is in the returned
array, then the key exists. Something like this:

Dim HKLM, Path, iRet, Reg, AKeys, sKey, i2, sErr
HKLM = &H80000002
Path = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
sKey = "{388E4B09-3E71-4649-8921-F44A3A2954A7}"

Set Reg = GetObject("winMgMts:root\default:StdRegProv")
iRet = Reg.EnumKey(HKLM, Path, AKeys)

On Error Resume Next
Select Case iRet
Case 0
If UBound(AKeys) = -1 Then
sErr = "no subkeys" 'no subkeys
Else
' There are subkeys to be checked.
End If
Case 2
sErr = "path invalid" '-- invalid key Path
Case -2147217405
sErr = "access denied" '-- access denied H80041003
Case Else
sErr = "Error number: " & iRet '-- some other error.
End Select

If Len(sErr) = 0 Then
For i2 = 0 to UBound(AKeys)
If UCase(AKeys(i2)) = UCase(sKey) Then
MsgBox "key exists"
Set Reg = Nothing
WScript.quit
End If
Next
End If

Set Reg = Nothing
MsgBox "Key does not exist. " & sErr



My System SpecsSystem Spec