Jump to content

Exiting Function mid function


Recommended Posts

I have a script that calls several functions. I am trying to add some debugging and error checking lines. For Example, this is something I am trying to do. i need a way to exit the function that I'm in inside of an "if" statement, without exiting the whole script...

Func _QuickTime()
;Declare Variables
    $szQuickTimeSettingSource = $szInstallFilesSource & 'QuickTime\QuickTime.qtp'
    $szQuickTimeSettingsDest = 'C:\Documents and Settings\All Users\Application Data\Apple Computer\QuickTime\QuickTime.qtp'
    $szQuickTimeInstallSource = $szInstallFilesSource & 'QuickTime\QuickTimeInstaller.exe'
    $szQuickTimeInstallDest = $szLocalTempDir & 'QuickTimeInstaller.exe'
    $szQuickTimeInstallString = 'C:\Temp\QuickTimeInstaller.exe /passive'
    $szQuickTimeProcessName = 'qttask.exe'
    $szQuickTimeFileCheck = 'C:\Program Files\QuickTime\QuickTimePlayer.exe'

    ;Copy Installer to local computer
    FileCopy($szQuickTimeInstallSource,$szQuickTimeInstallDest, 1)

    ;Run QuickTime Installer
        If FileExists($szQuickTimeInstallString) Then
    _RunDOS($szQuickTimeInstallString)
        Else
        DebugMessage('__QuickTime','Could not find: & $szQuickTimeInstallString)
        Exit Function ???                                     ;<<-------------------------------------- This is what I need!!!

    ;Copies Prefrences over, changes auto updates to off
    If $KMCDebug == 2 or 3 Then DebugMessage('__Quick Time: ','Copy preferences to local directory ...')
    FileCopy($szQuickTimeSettingSource,$szQuickTimeSettingsDest, 1)

    ;Check for process and close
    If $KMCDebug == 2 or 3 Then DebugMessage('__Quick Time: ','Check for qttask process and close it ...')
    If ProcessExists($szQuickTimeProcessName) Then ProcessClose($szQuickTimeProcessName)

    ;Write Registry settings and remove startup
    If $KMCDebug == 2 or 3 Then DebugMessage('__Quick Time: ','Write registry entry and remove from startup ...')
    RegWrite('HKEY_LOCAL_MACHINE\SOFTWARE\Apple Computer, Inc.\QuickTime\ActiveX\','QTTaskRunFlags', 'REG_DWORD', 2)
    RegDelete('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\','QuickTime Task')

    ;Delete installer
    If $KMCDebug == 2 or 3 Then DebugMessage('__Quick Time: ','Remove Installer ...')
    FileDelete($szQuickTimeInstallDest)

EndFunc
Link to comment
Share on other sites

Check out Return (to exit a function) and ExitLoop (to exit a loop) in the help file.

/edit: It's Return, not Return() :whistle:

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Return, so...

Func FileCheck()
If FileExists('c:\windows\win.ini') Then
   Return 0
Else
   Return 1
EndFunc

How do I evaluate what was returned??? LIke:

If FileCheck returned 0 then...do...

If FileCheck returned 1 the....do.... Not sure how to get that "return 0 or 1"

Does the following examply show enough?

<... code ...>
If _QuickTime() Then
    MsgBox(0, "test", "_QuickTime succeeded because _QuickTime returned true")
Else
    MsgBox(16, "ERROR", "_QuickTime FAILED!")
EndIf

MsgBox(0, "test", "By the way, 3 plus 5 is: " & _AddTwoNumbers(3, 5))

Exit

Func _QuickTime()
;<... do some QT code ...>
    If $everythingWorked Then
        Return True
    Else
        Return False
    EndIf
EndFunc  ;==>_QuickTime

Func _AddTwoNumbers($a, $b)
    Return $a + $b
EndFunc  ;==>_AddTwoNumbers

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Assign the function call to a variable, the return value is stored in that variable.

$call = FileCheck()
MsgBox(0, "Return value", "Function returns: " & $call)

Func FileCheck()
    If FileExists('c:\windows\win.ini') Then
       Return 0
    Else
       Return 1
   EndIf
EndFunc

You were missing an EndIf as well...

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

yay! Thx guys! This is what I got!

$FileCheckCall = _FileCheck('Star Navigator',$szStarMSIInstallString,'2',$StarFileCheck)
    If $FileCheckCall == 0 Then
        RunWait(@ComSpec & ' /c msiexec.exe /i ' & '"' & $szStarMSIInstallString & '" ' & $szStarMSIOptions)
    Else
        Return
    EndIf

Func _FileCheck($app,$filepath,$logtype,$logcheck)
    If FileExists($filepath) Then
        Return 0
    Else
        DebugMessage('!!__' & $Appname & ': ','Could not find: ' & $filepath)
        GLOBAL $FailedFlag = 1
        _LogCheck($LogType,$AppName,$LogCheck)
        Return 1
    EndIf
EndFunc
Link to comment
Share on other sites

yay! Thx guys! This is what I got!

$FileCheckCall = _FileCheck('Star Navigator',$szStarMSIInstallString,'2',$StarFileCheck)
    If $FileCheckCall == 0 Then
        RunWait(@ComSpec & ' /c msiexec.exe /i ' & '"' & $szStarMSIInstallString & '" ' & $szStarMSIOptions)
    Else
        Return
    EndIf

Func _FileCheck($app,$filepath,$logtype,$logcheck)
    If FileExists($filepath) Then
        Return 0
    Else
        DebugMessage('!!__' & $Appname & ': ','Could not find: ' & $filepath)
        GLOBAL $FailedFlag = 1
        _LogCheck($LogType,$AppName,$LogCheck)
        Return 1
    EndIf
EndFunc

Looks ok enough :whistle:

Except for one important thing: it is a very solid and whidespread convention to use 0 for false and not 0 for true or return value... Try:

$var1 = 0
$var2 = 5
$var3 = -1

If $var1 Then MsgBox(0,"test","$var1 = 0 so this messagebox should not show, by default 0 is false so if used in a boolean expression like this if-check, it produces a false.")

If $var2 Then MsgBox(0,"test","$var2 = 5 so this messagebox should show, by default other-than-0 is true.")

If $var3 Then MsgBox(0,"test","$var3 = -1 so this messagebox should show, by default other-than-0 is true.")

In other words, it is cleaner code if you follow this and return 0 for failure and 1 for success, also it makes for easier conditional statements like 'if'.

If you swap the 0 and 1 returning, you can change

If $FileCheckCall == 0 Then

for

If $FileCheckCall Then
; <- edit: got it wrong the first time :">

Looks better, reads easier.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

So you recommend that I change it to this? To keep with Autoit general standards? Does this look better?

$FileCheckCall = _FileCheck($szStarDisplayName,$szStarMSIInstallString,'2',$StarFileCheck)
    If $FileCheckCall Then
        If $KMCDebug == 2 or 3 Then DebugMessage('__' & $szStarDisplayName & ': ','Running install string ...')
        RunWait(@ComSpec & ' /c msiexec.exe /i ' & '"' & $szStarMSIInstallString & '" ' & $szStarMSIOptions)
    Else
        If $KMCDebug == 2 or 3 Then DebugMessage($szStarDisplayName & ': ','Exiting Function')
        Return
    EndIf


Func _FileCheck($app,$filepath,$logtype,$logcheck)
    If FileExists($filepath) Then
        Return 1
    Else
        DebugMessage('!!__' & $app & ': ','Could not find: ' & $filepath)
        GLOBAL $FailedFlag = 1
        Return 0
    EndIf
EndFunc
Link to comment
Share on other sites

So you recommend that I change it to this? To keep with Autoit general standards? Does this look better?

$FileCheckCall = _FileCheck($szStarDisplayName,$szStarMSIInstallString,'2',$StarFileCheck)
    If $FileCheckCall Then
        If $KMCDebug == 2 or 3 Then DebugMessage('__' & $szStarDisplayName & ': ','Running install string ...')
        RunWait(@ComSpec & ' /c msiexec.exe /i ' & '"' & $szStarMSIInstallString & '" ' & $szStarMSIOptions)
    Else
        If $KMCDebug == 2 or 3 Then DebugMessage($szStarDisplayName & ': ','Exiting Function')
        Return
    EndIf
Func _FileCheck($app,$filepath,$logtype,$logcheck)
    If FileExists($filepath) Then
        Return 1
    Else
        DebugMessage('!!__' & $app & ': ','Could not find: ' & $filepath)
        GLOBAL $FailedFlag = 1
        Return 0
    EndIf
EndFunc

Yes. Only here you've got something else wrong:

If $KMCDebug == 2 or 3 Then

This If will ALWAYS return True since a variable is checked, and the "truth" of an existing value, namely 3, too (which is always true), so the continional code will always be ran.

Change it to:

If $KMCDebug == 2 or $KMCDebug == 3 Then

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

you sure? This is the code:

GLOBAL $KMCDebug = 2 ;1 = normal debug 2 = strong 3 = All Debug 0 = no debug

Right? or am I doing it all wrong for the intended purpose?

Yes, I'm sure :whistle:

Look at this example:

$a = 3
If $a = 4 or 5 Then
    MsgBox(0,"test","You wouldn't want this to be shown now, would you?")
EndIf

/edit: then, try this:

$a = 3
If $a = 4 or $a = 5 Then
    MsgBox(0,"test","This really isn't shown...")
EndIf
Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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