Jump to content

Reading registry keys that aren't there?


Recommended Posts

Sorry for my crappy subject title :D

Anyway, I would like to read several registry keys for a automating some stuff on my network, but the thing is that im not always sure if those keys are there. I would only like to the description and the key value to write to a file if it actually exists. Not so that I constantly have empty description strings due to the key not being there.

How do I accomplish this feat in Autoscript?

Thanks,

Dennis

Link to comment
Share on other sites

Success: Returns the requested registry value. @EXTENDED is set to the type of the value $REG_... . These types are defined in the "Constants.au3" include file. 
Failure: Returns "" and sets the @error flag: 
 1 if unable to open requested key 
 2 if unable to open requested main key 
 3 if unable to remote connect to the registry 
 -1 if unable to open requested value 
 -2 if value type not supported

So, you could

$tVal = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir")
If @error = 1 Then
    ;Don't write the file, no key could be opened
ElseIf Not @error AND $tVal <> "" Then
    ;Write file, Key could be opened
    ConsoleWrite ("Key value: " & $tVal & @CRLF)
EndIf
Link to comment
Share on other sites

Thanks, exactly what i needed!

1 question tho, how can I combine them all in one string? Only the ones that exist ofcourse :D

I'd recommend you to study arrays, but as a brief example:

Local $RegKeys[4]

;Just some random keys as a demonstration
$RegKeys[0] = RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir")
$RegKeys[1] = RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "Version")
$RegKeys[2] = RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers", "1")
$RegKeys[3] = RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers", "2")

;In a for loop
For $i = 0 to 3 step 1
    MsgBox (32, "Example", $RegKeys[$i])
Next

;Or, by themselves
MsgBox (32, "Example", $RegKeys[0])
MsgBox (32, "Example", $RegKeys[1])
MsgBox (32, "Example", $RegKeys[2])
MsgBox (32, "Example", $RegKeys[3])

Of course, you'd put in the error handling for each RegRead (the example I posted above this)

:o

Link to comment
Share on other sites

A couple of other things you want to look for in the help file are RegEnumKey() and RegEnumVal()

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

Thanks guys, but I can't seem to implement it with description and error handling.

Also the msgbox pops one up each array item, I would like them all in one string.

Would you have a small example of this?

Forgive me for my n00bness.

Regards,

Dennis

Link to comment
Share on other sites

can you give us an example of the actual keys you are tring to read and the results that you expect? Then perhaps we can show you some functional code.

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

Thanks guys, but I can't seem to implement it with description and error handling.

Also the msgbox pops one up each array item, I would like them all in one string.

Would you have a small example of this?

Forgive me for my n00bness.

Regards,

Dennis

Local $RegKeys[4]

Local $allKeys = ''

;Just some random keys as a demonstration

$RegKeys[0] = RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir")

$RegKeys[1] = RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "Version")

$RegKeys[2] = RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers", "1")

$RegKeys[3] = RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers", "2")

;In a for loop

For $i = 0 to 3 step 1

$allKeys &= $RegKeys[$i] & @CRLF

Next

MsgBox (0,"All Keys: ", $allKeys)

Using the &= operator is what you need to glue the strings together, the @CRLF adds a carriage return line feed to each string

just remove that if you want it to display as one really long line.

Cheers

Link to comment
Share on other sites

;In a for loop

For $i = 0 to 3 step 1

$allKeys &= $RegKeys[$i] & @CRLF

Next

You never need to use step 1 in a for loop. That is the default. The only time you need step if it is a number larger than 1 or if you want to step backwards then you use it with a negative value.

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

Hi BlackScorpion, GEOSoft,

Sadly due to the nature of the company i cant go into any specifics, but using these example registry keys is great.

Secondly, I notice that for example in BlackScorpions code that if i change it to a path that doesnt exist it blanks out. This is just what I don't want. The reason is that I want to build a list of what is there, not including whats not.

For example i want to get this kind of output:

Installdir: this_value

Version: 1.3.4.6

Timeserver1: ja.gfkld.com

Timeserver2. sfsd.dfsd.com

To clarify: this is a self written description (like Version:) with the value of that registry key.

However, if I take BlackScorpions code and the Timeserver 1 key isnt found, it will come out like this:

Installdir: this_value

Version: 1.3.4.6

Timeserver1:

Timeserver2. sfsd.dfsd.com

It will NULL out the value, but the description is still there! This might not be so bad in this example, but removing +-200 empty value descriptions is kind of a pain. So yo clarify my question:

1. how can I add custom description strings?

2. If a value isnt found, how can I make it not display the description too?

Link to comment
Share on other sites

$sOutput = ""

$sVal = RegRead($key, $val)

If $sVal Then $sOutPut &= $sVal

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

BTW the reason I wanted a list of keys was just so I could see if they were all in the same Hive or if the values were all in the same key. If Either is the case then RegEnumKey() or RegEnumVal() would be the way to go.

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

That code just displays how to make it conditonal.

I don't know what you are doing with those reg values after you read them.

We do Know that you are using RegRead() so if that is declared as a variable ($sVal in the code above) Then check if it's empty with either

If $sVal Then

or

If $sVal <> "" Then

After that do whatever it is that you are doing with the results. If the value is empty then do nothing with it.

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

this is what i use every time i read registry :

Func _ReportErrRegRead($ReturnValue)
    
    If $ReturnValue =  "" Then  
        Switch  @ERROR 
            Case 1 
                Return " unable to open requested key "
    
            Case 2 
                Return " unable to open requested main key "
                
            Case 3 
                Return " unable to remote connect to the registry "
                
            Case -1 
                Return " unable to open requested value "
                
            Case -2 
                Return " value type not supported "
                
            Case Else 
                Return " "
                
        EndSwitch
    EndIf

EndFuncoÝ÷ ØÚ0jYn«Þ¶hÆ®¶­sbb33c·&WEfÂÒ&Vu&VBgV÷C´´UôÄô4ÅôÔ4äRb3#µ4ôeEt$Rb3#´WFôBc2b3#´WFôBgV÷C²ÂgV÷C´ç7FÆÄF"gV÷C²¤6öç6öÆUw&FRõ&W÷'DW'%&Vu&VBb33c·&WEfÂ

this will display the error messages on to the console out put.

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...