Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Check for existence of a registry KEY (not value!)


  • Please log in to reply
3 replies to this topic
mbirth
  • Members
  • 40 posts
  • Last active: Jul 31 2008 11:12 AM
  • Joined: 03 Oct 2005
If you want to check for the existence of a registry key (not value), you might have noticed that there is no easy way since RegRead sets ErrorLevel to 1 if there's a problem and trying to read a non-defined (Default)-value of a key IS a problem.

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

Dougal
  • Members
  • 18 posts
  • Last active: Oct 09 2015 10:31 PM
  • Joined: 21 Nov 2013

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
}


AHKTeo14
  • Members
  • 9 posts
  • Last active: Jul 16 2018 09:32 PM
  • Joined: 09 Apr 2014

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



AHKTeo14
  • Members
  • 9 posts
  • Last active: Jul 16 2018 09:32 PM
  • Joined: 09 Apr 2014

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
}