syno Posted February 22, 2009 Share Posted February 22, 2009 Hi guys I have a FOR loop which reads each line of a text file and returns the results, in order to tell me if a particular string has been found or not. This is all well and good and it works, if I add a single string into the code, ie "The quick brown fox jumped over the lazy dog": Dim $line If Not _FileReadToArray("C:\Test.txt", $line) Then MsgBox(4096,"Error", " Error reading file. Please check the file name and start again:" & @error) Exit EndIf For $x = 1 to $line[0] If $line[$x] = "The quick brown fox jumped over the lazy dog" Then ConsoleWrite("String found at line " & $x) Else MsgBox(4096, "An error has been found at line" & $x & @CRLF) EndIf Next However my file has multiple lines of text. I need something that checks every line of text against a given value and returns an error if that value, that is hard coded into my script does not appear in the text file. So "The quick brown fox jumped over the lazy dog" will be the first line. The second line might be "The fox then disappeared into the bushes". How do I get my code to look for this second line of text. Do I need to start looking at arrays for this... Thank you Link to comment Share on other sites More sharing options...
Inverted Posted February 22, 2009 Share Posted February 22, 2009 I don't understand, you mean, you want to check for the existence of a line, and if it's there, start another check for a second/different string ? Link to comment Share on other sites More sharing options...
FireFox Posted February 22, 2009 Share Posted February 22, 2009 @syno hm...? maybe this #Include <File.au3> $s_string = 'text to search' For $i = 1 To _FileCountLines('C:\test.txt') $line = FileReadLine('C:\test.txt', $i) If StringInStr($line, $s_string) Then ConsoleWrite('+> $s_string found on line ' & $i & @CRLF) Else ConsoleWrite('!> $s_string not found on line ' & $i & @CRLF) EndIf Next Not tested. Cheers, FireFox. Link to comment Share on other sites More sharing options...
rabbitkillrun Posted February 22, 2009 Share Posted February 22, 2009 I don't understand, you mean, you want to check for the existence of a line, and if it's there, start another check for a second/different string ?that's what i understood them to be asking from the last bit... Link to comment Share on other sites More sharing options...
syno Posted February 23, 2009 Author Share Posted February 23, 2009 @syno hm...? maybe this #Include <File.au3> $s_string = 'text to search' For $i = 1 To _FileCountLines('C:\test.txt') $line = FileReadLine('C:\test.txt', $i) If StringInStr($line, $s_string) Then ConsoleWrite('+> $s_string found on line ' & $i & @CRLF) Else ConsoleWrite('!> $s_string not found on line ' & $i & @CRLF) EndIf Next Not tested. Cheers, FireFox. Hi, Thanks for this but it is not really what I am looking for. What I am trying to do is create an automated test script that will check each line of a text file to make sure it contains the correct text. So where '$s_string' will be the first line, I need something that will look at the second line and go ahh yes, this line exist and go onto the third line to make sure that it is correct. If the script comes across a line that is not correct it will flag an error message. I believe that I am getting there slowly but it seems like my script can only check the first line, and that it. It cannot check the second, third etc etc. I hope this makes sense Link to comment Share on other sites More sharing options...
Authenticity Posted February 23, 2009 Share Posted February 23, 2009 You can use an array and a counter for this task. The array should contain the lines that are get checked against the file and the counter may increase after a match. In the loop you can use a flag variable to check if after the first or second etc match there was one extra loop that didn't match the corresponding line the loop is interrupted and checking the counter if it's match the last subscript of the array (a match) or it is not. Link to comment Share on other sites More sharing options...
dantay9 Posted February 24, 2009 Share Posted February 24, 2009 Is this what you were looking for? Dim $Line[3] $Line[1] = "The quick brown fox jumped over the lazy dog" $Line[2] = "The brown fox." For $x = 1 To $Lines If FileReadLine("C:\Test.txt", $x) == $Line[$x] Then ConsoleWrite("String found at line " & $x) Else MsgBox(4096, "ERROR", "An error has been found at line # " & $x & @CRLF) EndIf Next Link to comment Share on other sites More sharing options...
syno Posted March 3, 2009 Author Share Posted March 3, 2009 You can use an array and a counter for this task. The array should contain the lines that are get checked against the file and the counter may increase after a match. In the loop you can use a flag variable to check if after the first or second etc match there was one extra loop that didn't match the corresponding line the loop is interrupted and checking the counter if it's match the last subscript of the array (a match) or it is not.Hi thereCould you possibly show me an example of this.....Thanks Link to comment Share on other sites More sharing options...
Aceguy Posted March 3, 2009 Share Posted March 3, 2009 (edited) #include <File.au3> Dim $check[4] $check[1] = "this is a" $check[2] = "very short" $check[3] = "test using 2 loops" For $i = 1 To _FileCountLines('C:\test.txt') $line = FileReadLine('C:\test.txt', $i) For $c = 1 To UBound($check) - 1 If StringInStr($line, $check[$c]) Then ConsoleWrite($ckeck[$c]&' +> $s_string found on line ' & $i & @CRLF) Else ConsoleWrite('!> $s_string not found on line ' & $i & @CRLF) EndIf Next Next Edited March 3, 2009 by Aceguy [u]My Projects.[/u]Launcher - not just for games & Apps (Mp3's & Network Files)Mp3 File RenamerMy File Backup UtilityFFXI - Realtime to Vana time Clock Link to comment Share on other sites More sharing options...
Authenticity Posted March 4, 2009 Share Posted March 4, 2009 (edited) expandcollapse popupDim $sText1 = 'Blah Blah' & @LF & 'Some more blah' & @LF & 'The quick brown' & @LF & _ 'fox jumped over' & @LF & 'the lazy dog' Dim $sText2 = 'Some more blah' & @LF & 'The quick brown' & @LF & 'fox jumped over' & @LF & _ 'the lazy dog' & @LF & 'Don''t bother to check this line' Dim $sText3 = 'Blah Blah' & @LF & 'Some more blah' & @LF & 'The quick brown' & @LF & _ 'fox jumped over' & @LF &'No macth here' & @LF & 'the lazy dog' Dim $aLines[3] = ['The quick brown', 'fox jumped over', 'the lazy dog'] If _Check($sText1, @LF) Then ConsoleWrite('Match on $sText1' & @LF) If _Check($sText2, @LF) Then ConsoleWrite('Match on $sText2' & @LF) If _Check($sText3, @LF) Then ConsoleWrite('Match on $sText3' & @LF) Func _Check($sStr, $Del) Local $fFirstMatch = False, $iCounter = 0 Local $i, $aLinesSplit = StringSplit($sStr, $Del) For $i = 1 To $aLinesSplit[0] If $fFirstMatch Then If $aLinesSplit[$i] = $aLines[$iCounter] Then $iCounter += 1 If $iCounter > 2 Then ExitLoop Else ExitLoop EndIf Else If $aLinesSplit[$i] = $aLines[$iCounter] Then $iCounter += 1 $fFirstMatch = True EndIf EndIf Next If $iCounter > 2 Then Return True Return False EndFuncDid you mean that the sequence of lines appear to be anywhere on the file but must follow each other?Like in the example the third get never matched because of the extra line between the 'fox jumped over' andthe 'the lazy dog'.Edit: Moi mistake ^^. I was so concentrated about this approach that I've completely ignored the RegExp approach which might be faster and much flexible in this case. Also, you'll need to fix the loop to continue with the rest of the text and not only after a first match is found. Edited March 4, 2009 by Authenticity Link to comment Share on other sites More sharing options...
syno Posted March 4, 2009 Author Share Posted March 4, 2009 (edited) Can you use multiple variables in 'StringInStr' for example: dim $s_string dim $s_string1 For $i = 1 To _FileCountLines('C:\test.txt') $line = FileReadLine('C:\test.txt', $i) IF StringInStr($line,$s_string, $s_string1]) THEN...... Thanks Edited March 4, 2009 by syno Link to comment Share on other sites More sharing options...
FireFox Posted March 4, 2009 Share Posted March 4, 2009 (edited) @syno just store you strings into an array for example : Local $a_array[50] ;store 50 variables (0 to 49) For $i = 0 to 49 $a_array[$i] = $i ;store $i into the array ConsoleWrite($a_array[$i] & @CRLF) Next Cheers, FireFox. Edited March 4, 2009 by FireFox Link to comment Share on other sites More sharing options...
syno Posted March 4, 2009 Author Share Posted March 4, 2009 @syno just store you strings into an array for example : Local $a_array[50] ;store 50 variables (0 to 49) For $i = 0 to 49 $a_array[$i] = $i ;store $i into the array ConsoleWrite($a_array[$i] & @CRLF) Next Cheers, FireFox. OK at the moment I have: #Include <File.au3> Dim $find = 'simon' Dim $find1 = 'says' Dim $find2 = 'boo' For $i = 1 To _FileCountLines('C:\test.txt') $line = FileReadLine('C:\test.txt', $i & @LF) If StringInStr($line, $find) & StringInStr($line, $find1) & StringInStr($line, $find2) Then MsgBox(0, 'FOUND', 'string found on line ' & $i & @CRLF) Else MsgBox(0, 'ERROR', 'string not found on line ' & $i & @CRLF) EndIf Next but at the moment the output I am getting just says: 'String found on line 1', 'String found on line 2' etc. However my code does not read the value of the variable it only recognises that there is a string present. Consequently I could have anything in the text file and all it would say is yes there is string so 'string found on line 1'. I need this to pick up 'simon' on line 1 and if the line does not have 'simon' then it will throw an error. I will look at using array's later, but for now I would like to assign some text to a variable. Thanks Link to comment Share on other sites More sharing options...
FireFox Posted March 4, 2009 Share Posted March 4, 2009 @syno Instead of using '&' in your 'If - Then' line, use the statement 'And' Cheers, FireFox. Link to comment Share on other sites More sharing options...
Authenticity Posted March 4, 2009 Share Posted March 4, 2009 I don't get your intention. Do you want this 3 words to appear anywhere on the current line or to appear each followed by the next one, yielding just to match 'simon says boo'? Your loop is checking for things like "simon boo sooooo says" and many more combinations (actually, logically infinite). Link to comment Share on other sites More sharing options...
syno Posted March 5, 2009 Author Share Posted March 5, 2009 I don't get your intention. Do you want this 3 words to appear anywhere on the current line or to appear each followed by the next one, yielding just to match 'simon says boo'?Your loop is checking for things like "simon boo sooooo says" and many more combinations (actually, logically infinite).HelloAt the moment the test file looks like this:simonsaysbooI need to check each line to make sure the text exists, so in this case the output would say: 'string found on line 1, 2, 3'. However if the text file read:simonERRORsaysbooThe output would read: string found on line 1, string not found on line 2, string found on line 3, string found on line 3.Thanks Link to comment Share on other sites More sharing options...
FireFox Posted March 5, 2009 Share Posted March 5, 2009 @synotry this :#include <File.au3> Local $find = 'simon', $find1 = 'says', $find2 = 'boo' For $i = 1 To _FileCountLines('C:\test.txt') $line = FileReadLine('C:\test.txt', $i) If StringInStr($line, $find) Or StringInStr($line, $find1) Or StringInStr($line, $find2) Then MsgBox(0, 'FOUND', 'string found on line ' & $i & @CRLF) Else MsgBox(0, 'ERROR', 'string not found on line ' & $i & @CRLF) EndIf NextCheers, FireFox. Link to comment Share on other sites More sharing options...
syno Posted March 5, 2009 Author Share Posted March 5, 2009 @ Firefox Thats great, you have been an enormous help ! Thanks to all who added to this thread happy me Link to comment Share on other sites More sharing options...
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