Sign in to follow this  
Followers 0
syno

Reading a text file

18 posts in this topic

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

Share this post


Link to post
Share on other sites



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 ?

Share this post


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


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


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

Share this post


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


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


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

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by Aceguy

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (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 by syno

Share this post


Link to post
Share on other sites

#12 ·  Posted (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 by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


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

Share this post


Link to post
Share on other sites

@syno

Instead of using '&' in your 'If - Then' line, use the statement 'And'

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


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

Share this post


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

Share this post


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


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

@ Firefox

Thats great, you have been an enormous help !

Thanks to all who added to this thread

happy me :P

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  
Followers 0