Sign in to follow this  
Followers 0
emendelson

For ... Next Until @error?

13 posts in this topic

This should be very simple to anyone who knows anything about loops, but I can't figure out the correct syntax. I'm trying to write a bit of code fragment that will look a a set of registry keys with names such as 7.4, 8.1, 11.1 , etc., and find out what the highest numerical value is. The trouble is that I don't know how to make the code continue reading values until it reaches the end, no matter how many values there are. I've worked around it by simply making the loop go from 1 to 20, but I'd rather allow for all possibilities. Can anyone help? Here's the code (which tests for the highest version of Ghostscript currently installed):

; test Ghostscript version
$gs1 = RegEnumKey("HKLM\Software\AFPL Ghostscript", 1)
If @error Then 
    MsgBox(4096,"GS Test", "No Ghostscript version installed")
    Exit
EndIf

$gs1num = Number($gs1)
For $i = 2 to 20
$gs2 = RegEnumKey("HKLM\Software\AFPL Ghostscript", $i)
    If @error Then 
        Call("Final")
    EndIf
    $gs2num = Number($gs2)
    If $gs2num > $gs1num Then $gs1 = $gs2
Next
Call("Final")

Func Final()
    MsgBox(4096, "GS Test", "Highest GS version is " & $gs1)
    Exit
    EndFunc

I realize this should be fixed so a different message is displayed if there's only one version, but at least this (sort of) works. Thanks for any help with that For...Next loop.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

i don't quite understand what your saying. but if i'm correct, you basicallly want something similar to excel's "COUNT(range)"

lol if thats the case, its not so much a loop problem, as a variable problem.

you just cant get the defined number of reg keys your dealing with, right?

im not sure there is a function for that. (Do I smell a UDF comming on? :):( )(one that can count what is in a specified list, is that UDF material?)

i dunno if "WinGetText" can help you. take a close look at the helpfile and see if you find anything

Edit:

I bet, if you want to enter reg keys yourself, you could use like

$keys = Inputbox("Input Reg keys', "Input reg keys separated by commas")
$keys = Stringsplit

but i guess that would defeat the purpuse of the script

Edited by Paulie

Share this post


Link to post
Share on other sites

one quick thing you don't need call to call a fucntion just type the function name with parantheses after example

final()

you may need call if your callit it with paramaters

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Do a loop like this, it will continue trying to read keys until the RegEnumKey errors out, at which point it will exit the While loop and call your function (you dont need Call() ):

; test Ghostscript version
Local $i = 2

$gs1 = RegEnumKey("HKLM\Software\AFPL Ghostscript", 1)
If @error Then
    MsgBox(4096, "GS Test", "No Ghostscript version installed")
    Exit
EndIf
$gs1num = Number($gs1)

While 1
    $gs2 = RegEnumKey("HKLM\Software\AFPL Ghostscript", $i)
    If @error Then ExitLoop
    $gs2num = Number($gs2)
    If $gs2num > $gs1num Then $gs1 = $gs2
    $i = $i + 1
WEnd
Final()

Func Final()
    MsgBox(4096, "GS Test", "Highest GS version is " & $gs1)
    Exit
EndFunc;==>Final
Edited by Simucal

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)

Share this post


Link to post
Share on other sites

Do a loop like this, it will continue trying to read keys until the RegEnumKey errors out, at which point it will exit the While loop and call your function (you dont need Call() ):

.....

That is exactly what I was looking for! Thank you! (Also, now I'll know how to do this in the future....)

Share this post


Link to post
Share on other sites

...but I can't figure out the correct syntax...

I see that you have what you wanted, but I see nothing wrong with the syntax that you used...

Won't the For/Next loop exit when it runs out of subkeys?

So, a loop from 2 to 20 is probably enough.

If you think that you might miss some subkeys, just add zeros:

2 to 200000000

; test Ghostscript version
$gs1 = RegEnumKey("HKLM\Software\AFPL Ghostscript", 1)
If @error Then
    MsgBox(4096, "GS Test", "No Ghostscript version installed")
    Exit
EndIf

$gs1num = Number($gs1)
For $i = 2 To 200000000
    $gs2 = RegEnumKey("HKLM\Software\AFPL Ghostscript", $i)
    If @error Then ExitLoop
    $gs2num = Number($gs2)
    If $gs2num > $gs1num Then $gs1 = $gs2
Next

MsgBox(4096, "GS Test", "Highest GS version is " & $gs1)

If you really think that there could be more subkeys than that,

then make it infinite and add your own index:

; test Ghostscript version
$gs1 = RegEnumKey("HKLM\Software\AFPL Ghostscript", 1)
If @error Then
    MsgBox(4096, "GS Test", "No Ghostscript version installed")
    Exit
EndIf

$gs1num = Number($gs1)
$i = 1
While 1
    $i = $i + 1
    $gs2 = RegEnumKey("HKLM\Software\AFPL Ghostscript", $i)
    If @error Then ExitLoop
    $gs2num = Number($gs2)
    If $gs2num > $gs1num Then $gs1 = $gs2
WEnd

MsgBox(4096, "GS Test", "Highest GS version is " & $gs1)
I changed a few other things also...

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Already posted the answer :)


AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)

Share this post


Link to post
Share on other sites

Already posted the answer :)

Simucal's answer was the one I needed - not because 20000 keys would be more than enough, but because I wanted the most elegant solution - and that While...Wend loop was exactly it. Thanks again, Simucal!

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Already posted the answer :)

I did attempt to acknowledge your post/code when I said to Edward, "I see that you have what you wanted..." Maybe I should have said, "I see that Simucal has already posted code that you liked."

Not that it really matters, but your post was not present when I hit the reply button... but I did see it in one of my many "post previews". (Yep, it took me almost an hour - off and on - to construct that post.)

So, all of that just to say that I saw your code, but I wanted to point out that adding a few characters to Edward's existing code was (IMO) better than a While/WEnd.

@Edward,

Elegance is such a subjective of a term. Some might prefer the two lines of code needed for that For/Next loop over the four lines needed for this While/WEnd loop... Either way, I'm glad that you got what you needed.

edit:typo

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Well, I normally use a For loop for RegEnumKey, though for elegance and simplicity, I would prefer an option such as FileReadLine has of AutoIncrementing as I see enumerating keys or values as a standard 1x1 step process.

Edited by MHz

Share this post


Link to post
Share on other sites

herewasplato,

I didn't mean to be or sound ungrateful, and apologies if I did. It's an interesting question whether the While...WEnd version is less elegant because it requires more lines. I see that it could be argued either way. I don't pretend to know anything about programming, but I *think* I prefer a solution that avoids arbitrary numbers - although I know it's not really rational if the arbitrary number allows for tighter code...

Thanks to everyone for their help and comments on this.

Share this post


Link to post
Share on other sites

I did attempt to acknowledge your post/code when I said to Edward, "I see that you have what you wanted..." Maybe I should have said, "I see that Simucal has already posted code that you liked."

Not that it really matters, but your post was not present when I hit the reply button... but I did see it in one of my many "post previews". (Yep, it took me almost an hour - off and on - to construct that post.)

So, all of that just to say that I saw your code, but I wanted to point out that adding a few characters to Edward's existing code was (IMO) better that a While/WEnd.

@Edward,

Elegance is such a subjective of a term. Some might prefer the two lines of code needed for that For/Next loop over the four lines needed for this While/WEnd loop... Either way, I'm glad that you got what you needed.

Yea, I didnt mean to offend you either herewasplato. I was just confused.


AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)

Share this post


Link to post
Share on other sites

I'm not offended, not at all...

It is I that should apologize to y'all for putting you thru my ramblings...

...but I'm going to do it again :-)

Looking back on my posts to this thread, I can see that I really did not write them well. I can see how you thought that I was mad/offended/whatever.

I have a tendency to explain "history" - like that little paragraph about how long I spent on the post. I know that such info can be taken the wrong way, but the posts have time stamps and I was attempting to explain the sequence of events on my end. I actually thought about not posting at all once I saw that "the answer" had been posted... but I was hoping to encourage Edward that "he done good" - from what I saw - for this script - the syntax was good.

More history:-)

I realized that Edward wanted a While/WEnd.

I coded and tested same with my MS Office keys.

...but as I was trimming down code like this:

If @error Then

Call("Final")

EndIf

to code like this:

If @error Then ExitLoop

[That got rid of two lines of code.]

...I actually backed out the:

$i = 1

While 1

$i = $i + 1

WEnd

in favor of just adding "0000000" to the existing For/Next code.

[That got rid of two lines of code that I had added.]

Then I saw Simucal's code and I rebuilt my version of the While/WEnd code to include in my post. See why it takes me an hour to post sometimes.

I need some sleep................................. thanks for enduring.


[size="1"][font="Arial"].[u].[/u][/font][/size]

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
Sign in to follow this  
Followers 0