EddardStark Posted August 17, 2009 Share Posted August 17, 2009 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. Link to comment Share on other sites More sharing options...
Genos Posted August 17, 2009 Share Posted August 17, 2009 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 Link to comment Share on other sites More sharing options...
EddardStark Posted August 17, 2009 Author Share Posted August 17, 2009 Nice. That looks simple enough. I'll give it a whirl. Thanks. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 17, 2009 Moderators Share Posted August 17, 2009 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 EndFuncDid 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Genos Posted August 17, 2009 Share Posted August 17, 2009 (edited) 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 August 17, 2009 by Genos Link to comment Share on other sites More sharing options...
Nutster Posted August 17, 2009 Share Posted August 17, 2009 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 NuttallNuttall 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... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now