Jump to content

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

Share this post


Link to post
Share on other sites

@Vikramjeet

What about Excel UDF? :)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

Share this post


Link to post
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 : HERE ( 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

 

Share this post


Link to post
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 : HERE ( 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

 

Share this post


Link to post
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

Share this post


Link to post
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 : HERE ( 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

 

Share this post


Link to post
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

Share this post


Link to post
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 : HERE ( 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

 

Share this post


Link to post
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 

Share this post


Link to post
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 : HERE ( 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

 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

  • Similar Content

    • By Eli_jahbot
      my esteemed autoits,
      i need your help once again. The bold and italics below are what i will be referring to. I need a message box to exit the script if Cancel is pressed and to continue the script if OK is pressed. I have tried multiple ways to do this to no avail. Can someone please breakdown how i go about this via Message box, and or Input box as I'm sure they will follow the same logic. I appreciate your time and assistance.
       
      Example:
      global $url, $username, $password, $sspassword
      $url = "https://securegateway.fairview.org"
      $username = "XXX"
      $password = "YYY"
      $sspassword = "ZZZ"
      #include <Constants.au3>
      #include <msgboxconstants.au3>
      ShellExecute ($url)
      WinWaitActive("window")
      send($username)
      send("{tab}")
      send($password)
      send("{enter}")
      msgbox($mb_okcancel, "Wait for the page to load then select OK to continue script", "Wait for the page to load then select OK to continue script")
      if($idok)
      send("+{tab 8}")
      send("{enter}")
      if ($idcancel) then Exit
      EndIf
    • By AlexFing17
      Hello, i am creating  a program that 
      1. it will check if the computer is connected to the internet 
      2. will display a message if it is connected 
      3.if not connected then it will continue to check until the computer connects to the internet then it runs the function which would have been executed if the computer was connected to the internet.
      what i tried.
      i connected my computer to the internet and run the code. Which displayed the msgbox that am connected
      i later turn my internet off and run the cold , i saw it running in process list.. i waited for 5 minutes and it was still waiting for internet connection (running), but when i turned the internet on and got internet connectivity, the app just close without displaying the msg that am connected.

      Please need help where i went wrong. or if anything should be added.
       
      this is the code
      #AutoIt3Wrapper_icon=1.ico $ping = Ping("www.google.com") If Not @error Then MsgBox("", "AlexFing", "You are connect") Else Do $ping = Ping("www.google.com") Until Not @error EndIf  
    • By zemkor
      Hello all, i have another question.
      My script not working, how is it correct ?
      Need script for this: If is Input 2 empty then call function odoslat() Else call function obrazok2()
      Thanks for help
       
       
      Func obrazok1() Local $oForm = _IEFormGetObjByName ($oIE, "formpridani") $obrazok1 = GUICtrlRead($Input1) $obrazok2 = GUICtrlRead($Input2) $oUpload = _IEGetObjByName($oForm, "souborp") Sleep (500) _IEAction($oUpload, "focus") Send("{SPACE}") Sleep (500) Send($obrazok1) Send("{Enter}") If $obrazok2 = (0) Then Call ("odoslat") Else Call ("obrazok2") EndIf EndFunc  
×
×
  • Create New...