nobby Posted December 31, 2003 Share Posted December 31, 2003 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 Link to comment Share on other sites More sharing options...
trids Posted December 31, 2003 Share Posted December 31, 2003 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 supportedHTH Link to comment Share on other sites More sharing options...
kami Posted December 31, 2003 Share Posted December 31, 2003 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 More sharing options...
GEOSoft Posted December 31, 2003 Share Posted December 31, 2003 (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 December 31, 2003 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 More sharing options...
kami Posted January 2, 2004 Share Posted January 2, 2004 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) Link to comment Share on other sites More sharing options...
trids Posted January 2, 2004 Share Posted January 2, 2004 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. 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 More sharing options...
willichan Posted April 10, 2009 Share Posted April 10, 2009 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. My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash Link to comment Share on other sites More sharing options...
GEOSoft Posted April 10, 2009 Share Posted April 10, 2009 (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 April 10, 2009 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 More sharing options...
willichan Posted April 13, 2009 Share Posted April 13, 2009 (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 -1I 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. 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 April 13, 2009 by willichan My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash Link to comment Share on other sites More sharing options...
microbious Posted May 1, 2009 Share Posted May 1, 2009 (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 May 1, 2009 by stoopid mr-es335 1 Link to comment Share on other sites More sharing options...
willichan Posted August 5, 2009 Share Posted August 5, 2009 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. My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash Link to comment Share on other sites More sharing options...
mr-es335 Posted July 22 Share Posted July 22 microbious, Thanks for this snippet...very much appreciated! mr-es335 Sentinel Music Studios Link to comment Share on other sites More sharing options...
Developers Jos Posted July 23 Developers Share Posted July 23 no need for this after 14 years! mr-es335 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Recommended Posts