Jump to content

Reading a text file


syno
 Share

Recommended Posts

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

@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

@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

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

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

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 there

Could you possibly show me an example of this.....

Thanks

Link to comment
Share on other sites

#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 by Aceguy
Link to comment
Share on other sites

Dim $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
EndFunc

Did 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' and

the '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 by Authenticity
Link to comment
Share on other sites

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 by syno
Link to comment
Share on other sites

@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

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

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).

Hello

At the moment the test file looks like this:

simon

says

boo

I 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:

simon

ERROR

says

boo

The 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

@syno

try 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
Next

Cheers, FireFox.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...