Jump to content
Sign in to follow this  
emartyn

Read a text file, search for a string, return 2 lines down - How to Loop?

Recommended Posts

emartyn

Hi all,

Newb here and looking for some help from someone(s) smarter than myself.

I need to scan a text file and search for a particular string. I then would need to know if the following 2nd line after the string is blank (actually there is a "Space" character). All I've got so far is the following:

#include <Array.au3>

#include <File.au3>

Func FindStringThenSkip($sPath, $FindGetNextLine)

Local $nArray

Local $GetLine

_FileReadToArray($sPath, $nArray)

For $i = 1 To UBound($nArray) - 1

If StringInStr($nArray[$i], $FindGetNextLine) Then

If $i < UBound($nArray) - 1 Then $GetLine = $GetLine & $nArray[$i+2] & Chr(01)

EndIf

Next

Return StringSplit(StringTrimRight($GetLine, 1), Chr(01))

EndFunc

Dim $Count

$run=0

$sPath = 'c:\log.txt'

$TextToFind = 'File(s) found - Initiating SFTP transfer for'

$Count = $Count + 1

$MainArray = FindStringThenSkip($sPath, $TextToFind)

If IsArray($MainArray) Then

If $Count <= UBound($MainArray) - 1 And $Count > 0 AND $MainArray[$Count]==" " Then MsgBox(0, "Result", "Error Detected")

EndIf

This works however it only finds the first instance of the string then stops, how can I make it loop until the end of the text file?

Thanks in advance!

Share this post


Link to post
Share on other sites
Melba23

emartyn,

Welcome to the AutoIt forum. :)

I would do it this way: :D

#include <Array.au3>
#include <File.au3>

$sPath = "log.txt"
$sTextToFind = "File(s) found - Initiating SFTP transfer for"
Global $aArray

; Get file into array
_FileReadToArray($sPath, $aArray)

; Start at the beginning
$iStart = 0

While 1

    ; Look for the line
    $iIndex = _ArraySearch($aArray, $sTextToFind, $iStart)
    
    ; If no matches than exit loop
    If $iIndex = -1 Then ExitLoop

    ; We have found a match so look at the second line down
    $sLine = $aArray[$iIndex + 2]
    ; See if it is a space
    If $sLine = " " Then
        MsgBox(0, "Hit", "Found " & $sTextToFind & " on line " & $iIndex & @CRLF & "2 lines below is a space")
    Else
        MsgBox(0, "Hit", "Found " & $sTextToFind & " on line " & $iIndex & @CRLF & "2 lines below is NOT a space")
    EndIf
    
    ; Restart the search from 3 lines down
    $iStart = $iIndex + 3
    ; But check if it is past the end of the array
    If $iStart > $aArray[0] Then ExitLoop

WEnd

I tested it on this file and it worked perfectly: ;)

1
2
3
File(s) found - Initiating SFTP transfer for
4
5
6
7
File(s) found - Initiating SFTP transfer for
9
 
1
2
3
File(s) found - Initiating SFTP transfer for
4
5
6
7

Please ask if anything is unclear. ;)

M23

P.S. When you next post code please use Code tags. Put [autoit] before and [/autoit] after your posted code. :D


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
emartyn

Thanks very much for your help. I got it working by hybridizing both scripts. Much appreciated!

#include <Array.au3>
#include <File.au3>


Func FindStringThenSkip($sPath, $FindGetNextLine)
    Local $nArray
    Local $GetLine
    _FileReadToArray($sPath, $nArray)
    For $i = 1 To UBound($nArray) - 1
        If StringInStr($nArray[$i], $FindGetNextLine) Then
            If $i < UBound($nArray) - 1 Then $GetLine = $GetLine & $nArray[$i+2] & Chr(01)
        EndIf
    Next
    Return StringSplit(StringTrimRight($GetLine, 1), Chr(01))
EndFunc

$file=FileOpen("C:\report.txt",1)
While 1
Dim $Count
$run=0
$sPath = 'c:\log2.txt'
$TextToFind = 'File(s) found - Initiating SFTP transfer for'
$Count = $Count + 1
$MainArray = FindStringThenSkip($sPath, $TextToFind)
    If IsArray($MainArray) Then 
      If $Count <= UBound($MainArray) - 1 And $Count > 0 AND $MainArray[$Count]==" " Then FileWriteLine ( $file, "Error")
           EndIf
If $Count > $MainArray[0] Then ExitLoop
WEnd
FileClose($file)

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
Sign in to follow this  

×