dirty Posted February 4, 2010 Share Posted February 4, 2010 Stuck with my code and need to end function if @error expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #RequireAdmin Opt("TrayOnEventMode",1) Opt("TrayMenuMode",1) IsAdmin ( ) $Form1 = GUICreate("hosts Editor",200,440) $hosts = (@WindowsDir & "\System32\drivers\etc\hosts") $OpenFile = FileOpen (@ScriptDir & "\Saved\hosts", 0+1) $fileLocalRead = FileRead(@ScriptDir & "\Saved\hosts") GUICtrlCreateLabel ('Example:',0,6,42) GUICtrlCreateinput ("127.0.0.1 www.google.com",43,3,157) $Input = GUICtrlCreateEdit ($fileLocalRead,0,30,200,370) $Button = GUICtrlCreateButton ("Save",0,400,100,40) $ButtonCancel = GUICtrlCreateButton ("Close",100,400,100,40) TrayCreateItem('Modify "HOSTS"') TrayItemSetOnEvent(-1,"ModifyFunction") ;function name TrayCreateItem('Exit') TrayItemSetOnEvent(-1,"ExitFunction") ;function name TraySetState() While 1 ;empty loop WEnd Func ModifyFunction() ;check if file exist localy. If not, copy it to local and only then start reading it and displaying it in edit box $checkIfLocalExist = FileExists (@ScriptDir & "\Saved\hosts") ;Check if hosts is previously saved into saved folder If $checkIfLocalExist = 0 Then ;if not, then $CopyHostsToLocal = FileCopy ($hosts,@ScriptDir & "\Saved\hosts",0+8) ;copy hosts from system to saved folder If @error Then MsgBox (16,"ERROR","Error Copying hosts File, it must be in use") Escape() ;Go to end of the function is error EndIf $hosts = (@WindowsDir & "\System32\drivers\etc\hosts") $fileLocalRead = FileRead(@ScriptDir & "\Saved\hosts") GUICtrlSetData ($Input, $fileLocalRead) $OpenFile = FileOpen (@ScriptDir & "\Saved\hosts", 0+1) GUISetState(@SW_SHOW, $Form1) While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE GUISetState(@SW_HIDE, $Form1) ExitLoop Case $Button FileOpen (@ScriptDir & "\Saved\hosts", 2) $readAgain = GUICtrlRead ($Input) $write = FileWrite ($OpenFile, $readAgain) FileClose ($OpenFile) $replacehost = FileCopy (@ScriptDir & "\Saved\hosts", @WindowsDir & "\System32\drivers\etc\hosts", 1) If $write = 1 and $replacehost = 1 Then MsgBox(0,'','Saved') Else MsgBox(0,'','Error writing') Endif Case $ButtonCancel GUISetState(@SW_HIDE, $Form1) ExitLoop EndSwitch WEnd Func Escape() ;end of the function MsgBox(0,'','Try Again') EndFunc EndFunc Func ExitFunction() Exit EndFunc As soon as i added If @error Escape() ;Go to end of the function is error Endif Func Escape() ;end of the function MsgBox(0,'','Try Again') EndFunc i get error saying: syntax error Func Why ? Do i have limits naming functions or something ? Link to comment Share on other sites More sharing options...
omikron48 Posted February 5, 2010 Share Posted February 5, 2010 (edited) You can't define functions in a function. You should bring the Func declaration outside. expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #RequireAdmin Opt("TrayOnEventMode",1) Opt("TrayMenuMode",1) IsAdmin ( ) $Form1 = GUICreate("hosts Editor",200,440) $hosts = (@WindowsDir & "\System32\drivers\etc\hosts") $OpenFile = FileOpen (@ScriptDir & "\Saved\hosts", 0+1) $fileLocalRead = FileRead(@ScriptDir & "\Saved\hosts") GUICtrlCreateLabel ('Example:',0,6,42) GUICtrlCreateinput ("127.0.0.1 www.google.com",43,3,157) $Input = GUICtrlCreateEdit ($fileLocalRead,0,30,200,370) $Button = GUICtrlCreateButton ("Save",0,400,100,40) $ButtonCancel = GUICtrlCreateButton ("Close",100,400,100,40) TrayCreateItem('Modify "HOSTS"') TrayItemSetOnEvent(-1,"ModifyFunction") ;function name TrayCreateItem('Exit') TrayItemSetOnEvent(-1,"ExitFunction") ;function name TraySetState() While 1 ;empty loop WEnd Func ModifyFunction() ;check if file exist localy. If not, copy it to local and only then start reading it and displaying it in edit box $checkIfLocalExist = FileExists (@ScriptDir & "\Saved\hosts") ;Check if hosts is previously saved into saved folder If $checkIfLocalExist = 0 Then ;if not, then $CopyHostsToLocal = FileCopy ($hosts,@ScriptDir & "\Saved\hosts",0+8) ;copy hosts from system to saved folder If @error Then MsgBox (16,"ERROR","Error Copying hosts File, it must be in use") Escape() ;Go to end of the function is error EndIf $hosts = (@WindowsDir & "\System32\drivers\etc\hosts") $fileLocalRead = FileRead(@ScriptDir & "\Saved\hosts") GUICtrlSetData ($Input, $fileLocalRead) $OpenFile = FileOpen (@ScriptDir & "\Saved\hosts", 0+1) GUISetState(@SW_SHOW, $Form1) While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE GUISetState(@SW_HIDE, $Form1) ExitLoop Case $Button FileOpen (@ScriptDir & "\Saved\hosts", 2) $readAgain = GUICtrlRead ($Input) $write = FileWrite ($OpenFile, $readAgain) FileClose ($OpenFile) $replacehost = FileCopy (@ScriptDir & "\Saved\hosts", @WindowsDir & "\System32\drivers\etc\hosts", 1) If $write = 1 and $replacehost = 1 Then MsgBox(0,'','Saved') Else MsgBox(0,'','Error writing') Endif Case $ButtonCancel GUISetState(@SW_HIDE, $Form1) ExitLoop EndSwitch WEnd EndFunc Func Escape() ;end of the function MsgBox(0,'','Try Again') EndFunc Func ExitFunction() Exit EndFunc EDIT: Also learn to use proper indentation for nested statements so your code is not an eyesore to look at and a brain twister to read. Edited February 5, 2010 by omikron48 Link to comment Share on other sites More sharing options...
Minikori Posted February 5, 2010 Share Posted February 5, 2010 If @error Then Return 0 For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com Link to comment Share on other sites More sharing options...
BrettF Posted February 5, 2010 Share Posted February 5, 2010 Even better If @error Return SetError (1, 2, 3) Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Minikori Posted February 5, 2010 Share Posted February 5, 2010 (edited) Either way, much simpler than what you tried :| Edited February 5, 2010 by Minikori For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com Link to comment Share on other sites More sharing options...
dirty Posted February 5, 2010 Author Share Posted February 5, 2010 (edited) EDIT: Also learn to use proper indentation for nested statements so your code is not an eyesore to look at and a brain twister to read.What do you mean, variable names or what ? I thought variable names can be anything i want Then where do i get appropriate names ? or am i doing something else wrong ?If @error Return SetError (1, 2, 3) ?then what happens ? Would it jump to the end of current function ?Thanks guys Edited February 5, 2010 by dirty Link to comment Share on other sites More sharing options...
Developers Jos Posted February 5, 2010 Developers Share Posted February 5, 2010 (edited) If @error Return SetError (1, 2, 3) ?then what happens ? Would it jump to the end of current function ?It returns to the statement that called this Func. Edited February 5, 2010 by Jos 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...
omikron48 Posted February 5, 2010 Share Posted February 5, 2010 (edited) I meant follow coding convention when you post code online, like indenting lines depending on their nest level in the code so it's easier to follow. Kinda like: If <condition> Then <statement1> ... <statementn> EndIf Or If <condition1> Then <statement1> If <condition2> Then <statementA> ... <statementZ> EndIf ... <statementn> EndIf Doing so makes it clear which lines of code are inside conditionals, loops or function declarations. Edited February 5, 2010 by omikron48 Link to comment Share on other sites More sharing options...
dirty Posted February 6, 2010 Author Share Posted February 6, 2010 I meant follow coding convention when you post code online, like indenting lines depending on their nest level in the code so it's easier to follow.Doing so makes it clear which lines of code are inside conditionals, loops or function declarations.Ah ok, i thought something else was wrong. Its just the way i like thing, all aligned to the left but OK Link to comment Share on other sites More sharing options...
GEOSoft Posted February 6, 2010 Share Posted February 6, 2010 It returns to the statement that called this Func.Actually it would fail entirely with an error because of the missing "Then" 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...
dirty Posted February 7, 2010 Author Share Posted February 7, 2010 Actually it would fail entirely with an error because of the missing "Then"hhmm interestin.You mean it would jump back to If @error Then MsgBox (16,"ERROR","Error Copying hosts File, it must be in use")? sorry im not sure what statement is Link to comment Share on other sites More sharing options...
GEOSoft Posted February 7, 2010 Share Posted February 7, 2010 hhmm interestin.You mean it would jump back to If @error Then MsgBox (16,"ERROR","Error Copying hosts File, it must be in use")? sorry im not sure what statement is What you wrote in this post is correct. Your first post had left out the "Then" keyword as inif @Error Return..Which will fail and throw a syntax error 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...
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