Jump to content

Loop if, else, then


Recommended Posts

$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible =  0
#include <MsgBoxConstants.au3>

WinActivate("MyApplication")    ;Make my application active
Local $hWnd = WinWaitActive("MyApplication", "", 5)     ;Waits for my application to be the active application 

$oExcel.Application.WorkBooks.Open("C:\Users\Charlie\Desktop\Data.xlsx")    ;Opens an excel file
local $iCell = $oExcel.Application.Cells(31,1).Value    ;Reads the number from row 1, cell 31 of the open excel file

If $hWnd Then
   For $a = 1 to $iCell
      Send("G*S/")
      Send($oExcel.Application.Cells($a,1).Value)
      Send("'SET/US")
      Sleep(1000)
      Send("{ENTER}")
      Sleep(2000)
      Send("MB")
      Send("{ENTER}")
      Sleep(2000)
    
$file = @ScriptDir & "\DATA.log"

$iNum = StringRegExp(FileRead($file), "(?sm).*^\h*(\d{1,3})", 1)[0]     ;I am reading the last instance of 1-3 numeric values in the DATA.log file
ConsoleWrite($iNum & @CRLF)

;;;This is what I want to add - I also want to read the second last line of the DATA.log file and keep looking for an error 'Invalid Request'
;;;If I find the error 'Invalid Request' then I go back to the start -  If $hWnd Then
;;;If I do not find the error then proceed to the below steps

If $hWnd Then
    For $i = 1 To $iNum
        Send("GSET"& $i &"/USR")
        Sleep(500)
        Send("{ENTER}")
        Sleep(1000)
    Next
Else
    MsgBox(0, "", '"MyApplication" window not found.')
EndIf
Next
EndIf
$oExcel.Application.Quit

In the above script, I want to add a condition after 'ConsoleWrite($iNum & @CRLF)'. If I find the text 'Invalid Request' in the second last line of the active DATA.log file then I go back to the start of the loop. However if I do not find the text 'Invalid Request' in the second last line of the Data.log file then i proceed to the further steps

Link to comment
Share on other sites

@Vikramjeet
Since you were using

$oExcel.Application.WorkBooks.Open("C:\Users\Charlie\Desktop\Data.xlsx")

you could use

_Excel_BookOpen($strWorkbookName)

And...

50 minutes ago, Vikramjeet said:

I need to read a .log file.

It depends how your .log file is formatted, and what do you need to get from it.
Could you make a real world example? :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible =  0
#include <MsgBoxConstants.au3>

WinActivate("MyApplication")    ;Make my application active
Local $hWnd = WinWaitActive("MyApplication", "", 5)     ;Waits for my application to be the active application 

$oExcel.Application.WorkBooks.Open("C:\Users\Charlie\Desktop\Data.xlsx")    ;Opens an excel file
local $iCell = $oExcel.Application.Cells(31,1).Value    ;Reads the number from row 1, cell 31 of the open excel file

If $hWnd Then
   For $a = 1 to $iCell
      Send("G*S/")
      Send($oExcel.Application.Cells($a,1).Value)
      Send("'SET/US")
      Sleep(1000)
      Send("{ENTER}")
      Sleep(2000)
      Send("MB")
      Send("{ENTER}")
      Sleep(2000)
    
$file = @ScriptDir & "\DATA.log"

$iNum = StringRegExp(FileRead($file), "(?sm).*^\h*(\d{1,3})", 1)[0]     ;I am reading the last instance of 1-3 numeric values in the DATA.log file
ConsoleWrite($iNum & @CRLF)

;;;This is what I want to add - I also want to read the second last line of the DATA.log file and keep looking for an error 'Invalid Request'
;;;If I find the error 'Invalid Request' then I go back to the start -  If $hWnd Then
;;;If I do not find the error then proceed to the below steps

If $hWnd Then
    For $i = 1 To $iNum
        Send("GSET"& $i &"/USR")
        Sleep(500)
        Send("{ENTER}")
        Sleep(1000)
    Next
Else
    MsgBox(0, "", '"MyApplication" window not found.')
EndIf
Next
EndIf
$oExcel.Application.Quit

Attached is the log file. I want to read the second last line for the 'Invalid Request' response and then apply it in the script

DATA.log

Link to comment
Share on other sites

1 hour ago, IAMK said:

If you want an if-else-then inside a loop, then you can do something like:

While($continueLoop = True)
    If ... Then ;You want to quit the loop when this happens.
        $continueLoop = False
    ElseIf ... Then
        ...
    EndIf
WEnd

Replace ... with what you want.

While(1)
    If ... Then ;You want to quit the loop when this happens.
        ExitLoop
    ElseIf ... Then
        ...
    Else
        ExitLoop
    EndIf
WEnd

 

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

1 minute ago, Vikramjeet said:

Thank you. Once I Exit Loop, will this take me back to start of loop?

ExitLoop Mean you are exiting Loop :D 

So you can consider you will be at [WEnd] line after this.

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

1 minute ago, caramen said:

ExitLoop Mean you are exiting Loop :D 

So you can consider you will be at [WEnd] line after this.

If a condition is met, then I want to break (Not go through the following script) and go back to the start of the loop and run the loop again

Link to comment
Share on other sites

;DoStuff
While(1)
    If ... Then ;You want to quit the loop when this happens.
        CaseA () 
        ExitLoop ;< Will ExitLoop After CaseA () IsDone
    ElseIf ... Then
        CaseB () 
    Else
        ExitLoop
    EndIf
WEnd
;DoStuff

Func CaseA () 
    If ... Then 
        ...
    ElseIf ... Then
        ...
    Else
        ...
    EndIf
EndFunc

Func CaseB () 
    If ... Then 
        ...
    ElseIf ... Then
        ...
    Else
        ...
    EndIf
EndFunc

 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

3 minutes ago, caramen said:

ExitLoop Mean you are exiting Loop :D 

So you can consider you will be at [WEnd] line after this.

How can I tell the script to go to a specific position in the script and start from there instead of exiting the loop. I have loops within loops. Master loop has 2 loops within itself. So the master loop starts, then the sub loop starts, if a condition is met in the sub loop then I want the script to go to the start of the master loop

Link to comment
Share on other sites

1 minute ago, caramen said:
;DoStuff
While(1)
    If ... Then ;You want to quit the loop when this happens.
        CaseA () 
        ExitLoop ;< Will ExitLoop After CaseA () IsDone
    ElseIf ... Then
        CaseB () 
    Else
        ExitLoop
    EndIf
WEnd
;DoStuff

Func CaseA () 
    If ... Then 
        ...
    ElseIf ... Then
        ...
    Else
        ...
    EndIf
EndFunc

Func CaseB () 
    If ... Then 
        ...
    ElseIf ... Then
        ...
    Else
        ...
    EndIf
EndFunc

 

 

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible =  0
#include <MsgBoxConstants.au3>
#Include <File.au3>
Global $arlines

WinActivate("MyApplication")
Local $hWnd = WinWaitActive("MyApplication", "", 5)

$oExcel.Application.WorkBooks.Open("C:\Users\Charlie\Desktop\Data.xlsx")
local $iCell = $oExcel.Application.Cells(31,1).Value

$file = @ScriptDir & "\Capture.log"
Func Step1 ()       ;---> Master loop
If $hWnd Then
    For $a = 1 to $iCell
      Send("G*L")
      Send($oExcel.Application.Cells($a,1).Value)
      Send("/TIM")
      Sleep(1000)
      Send("{ENTER}")
      Sleep(2000)
;---------------------------------------------------------------------------
        _FileReadToArray ($file, $arlines)
            For $i = $arlines [0] To 1 Step - 1     ;---> Loop withing Master Loop
                If $arlines [$i] = "‡NONE LISTED" Then     
                Step1 ()        ;---> If text ‡NONE LISTED is found then go back to Func Step1, the Master Loop
                Else            ;Else do the below steps
                    Send("MB")
                    Send("{ENTER}")
                    Sleep(2000)
                    $iNum = StringRegExp(FileRead($file), "(?sm).*^\h*(\d{1,3})", 1)[0]
                    ConsoleWrite($iNum & @CRLF)
                EndIf
            For $i = 1 To $iNum     ; Loop Within Master Loop. Keep looping till value $iNum is reached and then go to Master Loop
            Send("GTIM"& $i &"/OVR")
            Sleep(500)
            Send("{ENTER}")
            Sleep(1000)
Next    
EndIf
$oExcel.Application.Quit
EndIf
FuncEnd

I have been working on this for 3 days now. Can someone please fix the loops and loops within loops in this script, The functions are working independently, however I am struggling with arranging the sequencing of the loops.....Thanks 

Link to comment
Share on other sites

17 minutes ago, Vikramjeet said:

I have been working on this for 3 days now. Can someone please fix the loops and loops within loops in this script, The functions are working independently, however I am struggling with arranging the sequencing of the loops.....Thanks 

Do you know there is a better way in stead of doing this :

If ... THen 
    If ... Then 
        If ... Then 
            If ... Then 
            
            
            
             EnfIf
        EndIf
    EndIf
EndIf

Just parse your script fine enouth for you. Then do separated condition.

 

If ... Then 

EndIf 

If ... Then 

EndIf

 

I realised sometime i use loop and loop again when one loop after one is just enouth.

 

If you encounter difficulty; Just rework your script.

 

Make more function.

 

You got

3 For

1 Next

1 If 2 EndIf

What the ..?

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

Try something like this.

$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = 0
#include <MsgBoxConstants.au3>
#include <File.au3>
Global $arlines

WinActivate("MyApplication")
Local $hWnd = WinWaitActive("MyApplication", "", 5)

$oExcel.Application.WorkBooks.Open("C:\Users\Charlie\Desktop\Data.xlsx")
Local $iCell = $oExcel.Application.Cells(31, 1).Value

$file = @ScriptDir & "\Capture.log"
While 1 ;---> Master loop
    If $hWnd Then
        For $a = 1 To $iCell
            Send("G*L")
            Send($oExcel.Application.Cells($a, 1).Value)
            Send("/TIM")
            Sleep(1000)
            Send("{ENTER}")
            Sleep(2000)
            ;---------------------------------------------------------------------------
            _FileReadToArray($file, $arlines)
            For $i = $arlines[0] To 1 Step -1 ;---> Loop withing Master Loop
                If $arlines[$i] = "‡NONE LISTED" Then
                    ContinueLoop 2 ;---> If text ‡NONE LISTED is found then go back to top of While loop, the Master Loop
                Else ;Else do the below steps
                    Send("MB")
                    Send("{ENTER}")
                    Sleep(2000)
                    $iNum = StringRegExp(FileRead($file), "(?sm).*^\h*(\d{1,3})", 1)[0]
                    ConsoleWrite($iNum & @CRLF)
                EndIf
                For $i = 1 To $iNum ; Loop Within Master Loop. Keep looping till value $iNum is reached and then go to Master Loop
                    Send("GTIM" & $i & "/OVR")
                    Sleep(500)
                    Send("{ENTER}")
                    Sleep(1000)
                Next
            Next
            $oExcel.Application.Quit
        Next
    EndIf
WEnd

But if I were you, I'd be looking at the _Excel UDF functions and run far away from using Send at all.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

52 minutes ago, BrewManNH said:

Try something like this.

$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = 0
#include <MsgBoxConstants.au3>
#include <File.au3>
Global $arlines

WinActivate("MyApplication")
Local $hWnd = WinWaitActive("MyApplication", "", 5)

$oExcel.Application.WorkBooks.Open("C:\Users\Charlie\Desktop\Data.xlsx")
Local $iCell = $oExcel.Application.Cells(31, 1).Value

$file = @ScriptDir & "\Capture.log"
While 1 ;---> Master loop
    If $hWnd Then
        For $a = 1 To $iCell
            Send("G*L")
            Send($oExcel.Application.Cells($a, 1).Value)
            Send("/TIM")
            Sleep(1000)
            Send("{ENTER}")
            Sleep(2000)
            ;---------------------------------------------------------------------------
            _FileReadToArray($file, $arlines)
            For $i = $arlines[0] To 1 Step -1 ;---> Loop withing Master Loop
                If $arlines[$i] = "‡NONE LISTED" Then
                    ContinueLoop 2 ;---> If text ‡NONE LISTED is found then go back to top of While loop, the Master Loop
                Else ;Else do the below steps
                    Send("MB")
                    Send("{ENTER}")
                    Sleep(2000)
                    $iNum = StringRegExp(FileRead($file), "(?sm).*^\h*(\d{1,3})", 1)[0]
                    ConsoleWrite($iNum & @CRLF)
                EndIf
                For $i = 1 To $iNum ; Loop Within Master Loop. Keep looping till value $iNum is reached and then go to Master Loop
                    Send("GTIM" & $i & "/OVR")
                    Sleep(500)
                    Send("{ENTER}")
                    Sleep(1000)
                Next
            Next
            $oExcel.Application.Quit
        Next
    EndIf
WEnd

But if I were you, I'd be looking at the _Excel UDF functions and run far away from using Send at all.

Thank you BrewManNH. ContinueLoop 2 is not taking the script to the master loop. Even when the specified text is found, it continues on with the next steps

Link to comment
Share on other sites

try using Exitloop. It might work better.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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

×
×
  • Create New...