Jump to content
Sign in to follow this  
EddardStark

GOTO Not Needed (v2 to v3 question)

Recommended Posts

EddardStark

I've been using AutoIT for a bit, but I've run into a bit of stumbling block. I used to use the GOTO command a lot in previous versions for scripts that required a machine to meet a combination of different variables. For instance:

If %AOSVERSION% = "Windows_2000", goto WIN2K.
If %AOSVERSION% = "Windows_XP", goto WINXP.


WIN2K:
If %other vairable% = "xyz", goto WIN2KXYZ
FileReadLine, script1, %logonserver%\\\\netlogon\\\\LoginScripts.txt, 1
(Etc - Other commands for this subsection)

How would I do that in version 3? I checked the help file for V2 users and it says the GOTO command isn't needed for V3. What's the easiest way to make a script that would run a different set of actions against a machine that has variables A,B,C,& D than a machine that has variables A,D,E,&F or a machine that has variables A,C,D,G, and so on.

Does the question make sense? I could provide some actual code of old scripts if needed. Thanks.

Share this post


Link to post
Share on other sites
Genos

It looks like you are just trying to use a function, if so you can just call that function and let it execute the function.

Try something like this:

If %AOSVERSION% = "Windows_2000" THEN WIN2k()
If %AOSVERSION% = "Windows_XP" THEN WINXP().


Func WIN2K()
If %other vairable% = "xyz" THEN WIN2KXYZ()
FileReadLine, script1, %logonserver%\\\\netlogon\\\\LoginScripts.txt, 1
(Etc - Other commands for this subsection)
EndFunc

Share this post


Link to post
Share on other sites
EddardStark

Nice. That looks simple enough. I'll give it a whirl. Thanks.

Share this post


Link to post
Share on other sites
Melba23

EddardStark,

Prepare yourself for a very steep learning curve - moving from a GOTO world into a GOTOless one is not a simple thing! But like all things it is possible - I would imagine many of the older members will have had to do it at some stage...not that I am implying you are amongst us of course! :(

You will have to adopt a new style of coding using Select and Switch to choose different routes to take, but with everything always returning to a core While...WEnd loop. This loop will act as the "Idle period" of the script where it waits for the next input - in MessageLoop mode using GUIGetMsg to read the control used; in OnEvent mode waiting for the next event. (And if that sounds like Greek than it is an indication of how much is different!)

That is why I said earlier that it requires a different approach to coding when you no longer have the GOTO function. Having made the transition, I am firmly of the camp that believes removing GOTO makes for better, more structured coding - although I found it quite difficult at first. if you are trying to port code directly from earlier versions of AutoIt, you are likely to find it very frustrating.

Your examples modified to give you a flavour of how they might look:

Switch @OSVersion
    Case "WIN_2000"
        win2k()   ; Run this function
    Case "WIN_XP"
        winxp()   ; Run this function
EndSwitch

Func win2k()

    Select 
        Case $other_variable = "xyz"
            WIN2KXYZ()  ; Run this function
        Case Else
            Do_Something_Else()  ; Run this function
    EndSelect

EndFunc

Did I mention that the syntax has changed a lot too? >_<

The best advice I can give is to think carefully about what it is you want your code to do (I find writing down a logic flow diagram a very useful exercise) and then restructure your code to fit how Autoit functions nowadays. Believe me, the current versions of AutoIt can certainly cope with whatever you want it do - the trick is to work with it and not against it! You will almost certainly not be able to work through the old code line by line! :(

Reading the Help file of the latest AutoIt version (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at the excellent tutorials that you will find here and here.

Good luck!

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Genos

Yes, using Switch/Case is a much prettier option, and will benefit you in the long run. When you come back to read your code it will be easier to understand >_

Edited by Genos

Share this post


Link to post
Share on other sites
Nutster

Another way of handling this is to directly embed the function code in-place, especially if it is used nowhere else. BTW, I prefer this indenting style, case in line with the Switch or Select.

Switch @OSVersion
Case "WIN_2000"
    Select 
    Case $other_variable = "xyz"
        WIN2KXYZ()  ; Run this function
    Case Else
        Do_Something_Else()  ; Run this function
    EndSelect
Case "WIN_XP"
    winxp()   ; Run this function
EndSwitch

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

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  

×