nobby

If exist

11 posts in this topic

Hey,

I was wondering if there is a function that checks for the existance of a registry key in the style

RegExist("keyname")

Cheers


CheersNobby

Share this post


Link to post
Share on other sites



Haven't tried it myself, but .. according to the helpfile, RegRead() returns the following values for @Error ..

  • 0 if success
  • 1 if Unable to open requested key
  • -1 if Unable to open requested value
  • -2 if Value type not supported
HTH

Share this post


Link to post
Share on other sites

If registry key does not exist, RegRead function will return 0.

So, if you are comparing non-REG_DWORD key with variable,

you will have to convert it to string....

But if you want only to check does corresponding key exist,

like "trids" said, @error can be used.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

If registry key does not exist, RegRead function will return 0.

Just the opposite. If the registry key and value exist it returns 0. To see if a key exists you can use RegRead for the Key and default value. For example

RegRead ("HKEY_CURRENT_USER\Applications\Explorer.exe\Drives","")
After that you would use error handleing to do what you want.

If @Error <>0 THEN RegWrite ("HKEY_CURRENT_USER\Applications\Explorer.exe\Drives,"","REG_SZ",)

Note: The code statements are all one line but may appear wrapped.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Just the opposite.  If the registry key and value exist it returns 0.

Try this.... You'll be suprised....

$Test = "test"
$Test = RegRead("HKEY_LOCAL_MACHINE\Something\Test", "")
MsgBox(0, "Test", $Test)

Share this post


Link to post
Share on other sites

Try this.... You'll be suprised....

Kami .. your example works as expected: the problem is that you should look at @error after the RegRead(). Not $Test. :whistle:

The following code highlights that RegRead() was Unable to open requested key .. because @error = 1

$Test = "test"
$Test = RegRead("HKEY_LOCAL_MACHINE\Something\Test", "")
MsgBox(0, "Test", @error & ";" & $Test)

Unless @error=0, $Test is meaningless

Share this post


Link to post
Share on other sites

Its a few years down the line, but just in case someone is trying to do this, the correct check would be

RegRead($RegKeyImLookingFor,"")
If @Error > 0 Then
   ;Key does not exist
    DoSomething()
Else
   ;Key does exist
    DoSomethingElse()
EndIf

Error codes <0 relate to values, not the key. You may get an error code <0, even if the key exists. You won't get a value related error if the key does not exist.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Its a few years down the line, but just in case someone is trying to do this, the correct check would be

RegRead($RegKeyImLookingFor,"")
If @Error > 0 Then
 ;Key does not exist
    DoSomething()
Else
 ;Key does exist
    DoSomethingElse()
EndIf

Error codes <0 relate to values, not the key. You may get an error code <0, even if the key exists. You won't get a value related error if the key does not exist.

You can't ignore -1 (Value does not exist). If you are checking default value and it exists but it's not set then the returned error is -1 Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

You can't ignore -1 (Value does not exist). If you are checking default value and it exists but it's not set then the returned error is -1

I appreciate the feedback. On more than one occasion, over the years, your posts have set me on the right track to find the solution I needed ... this thread included. :D

The idea here is to check for the existence of the key, not the value. Even if you do get a -1 result for an uninitialized "Default" value (I am curious as to what version of Windows you got that result from), you still know that the key exists. You wouldn't get a -1 or a -2 result if the key did not exist.

Edited by willichan

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

best example ever

RegRead ("HKEY_CURRENT_USER\test", "")
if @error = -1 Then
    MsgBox (0,"","yes it does")
ElseIf @Error = 1 Then 
    MsgBox (0,"","no it doesnt")
EndIf
Edited by stoopid

Share this post


Link to post
Share on other sites

best example ever

RegRead ("HKEY_CURRENT_USER\test", "")
if @error = -1 Then
    MsgBox (0,"","yes it does")
ElseIf @Error = 1 Then 
    MsgBox (0,"","no it doesnt")
EndIf

Gettingsmarter,

You not only miss critical values this way, but you also run an unnecessary If statement, which means a slower script.

Values of 0 or -2 can also indicate the existence of the key. You can assume that any positive value means that the key either does not exist, or is inaccessible. The cleanest and simplest method would be to use the code I submitted on Apr 10, 2009.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now