My approach is the following:
Loop %root%, %sub%, 1, 1 { keycount++ } If (keycount > 0) { ; ... key exists, do something with it } else { ; ... do something if the key doesn't exist }
Maybe there's a better way?
Cheers,
-mARKUS
Loop %root%, %sub%, 1, 1 { keycount++ } If (keycount > 0) { ; ... key exists, do something with it } else { ; ... do something if the key doesn't exist }
Found this (old) thread when I had same problem. Unfortunately this, and all other methods I found do not work if the key has no values or subkeys and default is not set. So I came up with this code which works for these cases as well:
RegKeyExist(sRoot, sKey) { RegExMatch(sKey, "(?<Key>(.+))\\(?<Subkey>(\w+))$", d) Loop, %sRoot%, %dKey%, 2, 0 if (A_LoopRegName = dSubkey) return, true }
Hi Dougal, i have the same problem.
You Code example is working fine, unless i have a space character in my Key.
The Key "HKEY_CURRENT_USER\Software\Netscape"
results "HKEY_CURRENT_USER\Software" as dKey and "Netscape" as dSubKey.
The Key "HKEY_CURRENT_USER\Software\Netscape\Netscape Navigateor" results nothing
this code seems to be working...
fnChk("HKEY_CURRENT_USER\Software\Netscape\Netscape Navigator","") fnChk("HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\Cache","") fnChk("HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\Cace","") fnChk("HKEY_CURRENT_USER\Console","") fnChk("HKEY_CURRENT_USER\Console","CursorSize") fnChk("HKEY_LOCAL_MACHINE\SYSTEM\Setup","SetupType") fnChk("HKEY_LOCAL_MACHINE\SOFTWARE\AutoHotkey","Version") ;// fnChk("","") ExitApp fnChk(vRegistryKey, vRegistryValue) { cQ := """" cR := "`n" if fnCheckKey(vRegistryKey, vRegistryValue) msgbox % "Result for Key/Value:" cR "Key: " cQ vRegistryKey cQ cR "Value: " cQ vRegistryValue cQ cR "= Exist" else msgbox % "Result for Key/Value:" cR "Key: " cQ vRegistryKey cQ cR "Value: " cQ vRegistryValue cQ cR "= not Exist" return } fnCheckKey(vREGKey,vREGValue = "") { local vResult, vRoot, vKey, dKey, dSubKey, cSlash := "\" StringLeft, vRoot,vREGKey, InStr(vREGKey,"\") - 1 StringRight, vKey,vREGKey, StrLen(vREGKey)-strlen(vRoot)-1 if(vREGValue) ;VALUE { RegRead, vResult, %vRoot%, %vKey%, %vREGValue% if ErrorLevel return, % false else return, % true } else ;KEY { vKey := fnCutSlash(vKey) if fnCountString(vKey, cSlash) > 0 { dSubKey := fnGetLastElement(vKey) dKey := fnLeftStringM(vKey,StrLen(dSubKey)+1) } else { dSubKey := vKey dKey= } Loop, %vRoot%, %dKey%, 2, 1 { if (A_LoopRegName = dSubkey) { return, true ;"key found" } } return, % false ;"Key not found" } } fnCutSlash(vRegKey) { if fnRightString(vRegKey,1) = "\" return % fnLeftStringM(vRegKey,1) return % vRegKey } fnCountString(vSourceString, vFindChar) { StringReplace, vSourceString, vSourceString, %vFindChar%,, UseErrorLevel Return ErrorLevel } fnGetLastElement(vPath) { return % fnRightString(vPath,instr(fnReverseString(vPath),"\")-1) } fnReverseString(vString) { local voutStr VarSetCapacity(voutStr, n:=StrLen(vString)) Loop %n% voutStr .= SubStr(vString, n--, 1) return voutStr } fnLeftStringM(vString,vLenght) { local vResult if(vLenght>=StrLen(vString)) vLenght:=0 vLenght := StrLen(vString) - vLenght StringLeft, vResult, vString, %vLenght% return, % vResult } fnRightString(vString,vLenght) { local vResult StringRight, vResult, vString, %vLenght% return, % vResult }