Jump to content

Reading a file


syno
 Share

Recommended Posts

Hi Peeps

I am trying to read a .txt document which has multiple lines of text. My script is shown below:

$file=FileRead("C:\test.txt")
$res=StringInStr($file,"The quick brown fox jumped over the lazy dog")
if $res=0 then
    MsgBox(0,"Sorry","String not found")
    Exit
EndIf
MsgBox(0,"Yes","We have found a string at "&$res)

However this script will only read the first line of the document called 'test.txt'. Is there any way to read every line of text from 'test.txt'.

I have looked at using

FileReadLine
, for this. Could somebody tell me if I am on the right lines.

no pun intended

Thanks

Link to comment
Share on other sites

Well, if your looking at reading multiple lines with just that code, it would only read one line then exit.

You need to have a loop to read each line.

Lets look at the helpfile on reading line form files...

This is the code in the example:

...
While  ;Eternal loop
    $line = FileReadLine($file)  ; Read the line of the file handle (Must be open in read mode) Note this automatically reads the next line each loop
    If @error = -1 Then ExitLoop ; If it is the end of the file, @error is set to -1, so exit the loop ending the reading.
    MsgBox(0, "Line read:", $line) ; show the read data
Wend...

So in conclusion, you just need to have a loop that reads the lines, then Exitloop at the end of the file.

Edited by disc330

Still learning...I love autoit. :)

Link to comment
Share on other sites

Sounds like you are on the right track... but FileRead should have read the entire file and put it all into one long string variable... if that is not the case you should try and figure out what the issue is there.

Overall, I think file readline is more along the lines of what you are wanting to do (assuming you want to do some kind of line replacement)

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

Try this:

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("C:\test.txt", $aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
   ; Msgbox(0,'Record:' & $x, $aRecords[$x])
    If $aRecords[$x] = "The quick brown fox jumped over the lazy dog" Then
        MsgBox(0, "", "Found the text on line " & $x)
    Else
        ConsoleWrite("The text was not found on line " & $x & @CRLF)
    EndIf
Next
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...