Jump to content
Sign in to follow this  
zvd

FileReadLine

Recommended Posts

Hello, in the following example, exclude.txt has 5 lines of text. The message box pops up 5 times to display each line of text. How can I make just one message box pop up with all 5 lines of text on one line?

Thanks|

$file = FileOpen("c:\temp\exclude.txt", 0)
If $file = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
   Exit
EndIf

While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
   MsgBox(0, "Test", $line)
WEnd
FileClose($file)

Share this post


Link to post
Share on other sites

Just store each line into a variable

Global $total
$file = FileOpen("c:\temp\exclude.txt", 0)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $total = $total & @CRLF & $line
WEnd

FileClose($file)

MsgBox(0, "Test", $total)

:)

Edit:

Code mistake fixed.

Edited by MHz

Share this post


Link to post
Share on other sites

You're example would only ever read the first line anyway. You never increase $line.

$sString = StringReplace(StringStripCR(FileRead("c:\temp\exclude.txt")), @LF, ' ')
MsgBox(64, 'Info', $sString)
The Carriage Returns/Line Feeds are replaced by spaces.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

$file = FileOpen("c:\temp\exclude.txt", 0)
Global $Line[6]
If $file = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
   Exit
EndIf

While 1
   $Message = ""
   For $i = 1 to 5
   $line[$i] = FileReadLine($file,$i)
   $Message= $Message&$Line[$i]&@CRLF
   Next
   If @error = -1 Then ExitLoop
   MsgBox(0, "Test", $message)
WEnd
FileClose($file)

Yeah, I was beat...

Share this post


Link to post
Share on other sites

i use file read to array

from help

#include <file.au3>
Dim  $aRecords
If Not _FileReadToArray("error.log",$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])
Next[/color]

8)

Edited by Valuater

Share this post


Link to post
Share on other sites

Yes, I tried working with that but had the same problem - trying to get just one message box with all the text on just one line (no CRLF's).

Share this post


Link to post
Share on other sites

Yes, I tried working with that but had the same problem - trying to get just one message box with all the text on just one line (no CRLF's).

I gave you the answer then.

http://www.autoitscript.com/forum/index.ph...st&p=296180


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

You're example would only ever read the first line anyway. You never increase $line.

Not true.

If he hadn't used FileOpen but given FileReadLine the filename instead, it's true however.

When FileReadLine is given a handle it loops through the file, line by line.

$sFile = FileOpenDialog("", "", "Text (*.*)")
If @error Then Exit

$hFile = FileOpen($sFile, 0)
If @error Then Exit

While 1
    $sLine = FileReadLine($hFile)
    If @error Then ExitLoop
    
    MsgBox(64, "", StringLeft($sLine, 40) & "...")
WEnd

MsgBox(64, "", "Finished")

Share this post


Link to post
Share on other sites

Not true.

If he hadn't used FileOpen but given FileReadLine the filename instead, it's true however.

When FileReadLine is given a handle it loops through the file, line by line.

$sFile = FileOpenDialog("", "", "Text (*.*)")
If @error Then Exit

$hFile = FileOpen($sFile, 0)
If @error Then Exit

While 1
    $sLine = FileReadLine($hFile)
    If @error Then ExitLoop
    
    MsgBox(64, "", StringLeft($sLine, 40) & "...")
WEnd

MsgBox(64, "", "Finished")
I can honestly say, I didn't... I didn't even know that :) ... thanks. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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  

×
×
  • Create New...