user3323320
user3323320

Reputation: 21

If Then statement in a loop

I'm creating a script that will open a program , login , and do specific tasks . I created the login script with a variables adding and loopin through . The problem is with the program , sometimes it just gives you an error on connection or a network problem . What I need is IF the script gets the problem it'll reset it self and go through the account it got the error on , This is what I have so far and I just can't make it work

$t = 0
$n = "account"
$p = "password"

For $r = X To X
    Run("program")

    AutoITSetOption("MouseCoordMode", 0)
    AutoITSetOption("WinTitleMatchMode", 3)

    Do
        Sleep(1000)
        $t = $t + 1
    Until WinActive("program") Or $t = 15

    $t = 0

    Sleep(1500)
    Send("{TAB}")
    Sleep(100)
    Send("{TAB}")
    Sleep(100)
    Send("{Enter}")
    Sleep(100)
    Send($n & $r)
    Sleep(200)
    Send("{TAB}")
    Sleep(200)
    Send($p & $r)
    Sleep(100)
    Send("{Enter}")
    Sleep(5500)

    If $t > 14 Then
        $r = $r - 1
        Run(@ComSpec & " /c taskkill /F /im program.exe")

        Do
            Sleep(500)
            $t = $t + 1
        Until WinActive("Program - Update News") Or $t = 15

        $t = 0
        WinActivate("Program")
        Sleep(2000)
        MouseClick("Primary", 28, 12)
        Sleep(1000)
        MouseClick("Primary", 35, 125)
        Sleep(1000)
        MouseClick("Primary", 360, 175)
        Sleep(2000)
        Send("{ENTER}")
        Sleep(2500)

        Run(@ComSpec & " /c taskkill /F /im program.exe")
    ; EndIf
Next

Right now what it does is reruns the program without actually closing the error window

Upvotes: 0

Views: 2836

Answers (1)

Mr. Hargrove
Mr. Hargrove

Reputation: 517

Hopefully this will give you a better idea of how to create a script and fix your problem. It is commented to give you an understanding of the steps taken.

#include <msgboxconstants.au3>

Local $t = 0, $n = "account", $p = "password" ; $n & $p are your own data
Local $r = ["X", "X", "X"], $e, $msg, $w  ;$r is filled with your own data

AutoItSetOption("MouseCoordMode", 0)
AutoItSetOption("WinTitleMatchMode", 3)

HotKeySet("{ESC}", "Quit") ;If you want to exit the script, press the ESC key
Sender()  ;call the function to run

While 1
    If $e <> 0 Then   ;if @error is set to a non-zero variable, wait 1 sec (1000 millisec) and run again
        Sleep(1000)
        Sender()
    ElseIf $w = 0 Then  ;if msgbox value is 0 AND retry OR ignore has been pressed, wait 1 second and run again
        Sleep(1000)
        $w += 1
        Sender()
    EndIf
WEnd


Func Sender()   ;wrapped in function to make returning out of the function easier
    For $r = "X" To UBound($r)  ; for the first value in $r to the length of the array

        Run("program") 
        $e = @error  ;if Run sets @error then put @error and check the value with an if statement
        If $e <> 0 Then
            $msg = MsgBox(2, "ERROR", "error running program, exiting...")
            If $msg = $IDABORT Then  ;if we don't want to run the program again we abort
                Quit()
            ElseIf $msg = $IDRETRY Then ;if we want to retry then we hit retry
                Return
            ElseIf $msg = $IDIGNORE Then ;if we want to retry then we hit ignore as well
                Return ;go out of the function because we have recieved an error and want to restart the function
            EndIf
        EndIf
        SetError(0) ;sets @error to 0 so that we don't accidentally restart our program 
        Do
            Sleep(1000)
            $t += 1  ;add 1 to $t
            Sleep(1500)
            Send("{TAB}")
            Sleep(100)
            Send("{TAB}")
            Sleep(100)
            Send("{Enter}")
            Sleep(100)
            Send($n & $r)
            Sleep(200)
            Send("{TAB}")
            Sleep(200)
            Send($p & $r)
            Sleep(100)
            Send("{Enter}")
            Sleep(5500)
            If $t > 14 Then
                $r -= 1 ;minus 1 from $r
                Run(@ComSpec & " /c taskkill /F /im program.exe")
                $e = @error
                If $e <> 0 Then
                    $msg = MsgBox(2, "ERROR", "error running " & @ComSpec & " /c taskkill /F /im program.exe, Abort to Quit; Retry & Ignore to RETRY.")
                If $msg = $IDABORT Then
                    Quit()
                ElseIf $msg = $IDRETRY Then
                    Return
                ElseIf $msg = $IDIGNORE Then
                    Return
                EndIf
            EndIf
        EndIf
        SetError(0)
    Until WinActive("program") Or $t = 15

    $t = 0   ;set $t to 0

    Do
        Sleep(500)
        $t += 1
        $w = WinActivate("Program")
        If $w = 0 Then
            $msg = MsgBox(2, "ERROR", "error activating program, Abort to Quit; Retry & Ignore to RETRY.")
            If $msg = $IDABORT Then
                Quit()
            ElseIf $msg = $IDRETRY Then
                Return
            ElseIf $msg = $IDIGNORE Then
                Return
            EndIf
        EndIf
        $w += 1
        Sleep(2000)
        MouseClick("Primary", 28, 12)
        Sleep(1000)
        MouseClick("Primary", 35, 125)
        Sleep(1000)
        MouseClick("Primary", 360, 175)
        Sleep(2000)
        Send("{ENTER}")
        Sleep(2500)
        Run(@ComSpec & " /c taskkill /F /im program.exe")
        $e = @error
        If $e <> 0 Then
            $msg = MsgBox(2, "ERROR", "error running " & @ComSpec & " /c taskkill /F /im program.exe, Abort to Quit; Retry & Ignore to RETRY.")
            If $msg = $IDABORT Then
                Quit()
            ElseIf $msg = $IDRETRY Then
                Return
            ElseIf $msg = $IDIGNORE Then
                Return
            EndIf
        EndIf
        SetError(0)
    Until WinActive("Program - Update News") Or $t = 15

Next

EndFunc   ;==>Sender

Func Quit() ;will be called when we want to completely stop the script
    Exit
EndFunc   ;==>Quit

Upvotes: 1

Related Questions