emartyn Posted July 13, 2011 Posted July 13, 2011 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!
Moderators Melba23 Posted July 13, 2011 Moderators Posted July 13, 2011 emartyn,Welcome to the AutoIt forum. I would do it this way: #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 WEndI 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 7Please ask if anything is unclear. M23P.S. When you next post code please use Code tags. Put [autoit] before and [/autoit] after your posted code. 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
emartyn Posted July 14, 2011 Author Posted July 14, 2011 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now