Jump to content

If exist


nobby
 Share

Recommended Posts

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
Link to comment
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.

Link to comment
Share on other sites

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!"

Link to comment
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

Link to comment
Share on other sites

  • 5 years later...

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.

Link to comment
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.

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!"

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 3 weeks later...
  • 3 months later...

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...