AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FilereadLine

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
motuslechat



Joined: 22 Oct 2004
Posts: 30

PostPosted: Thu Nov 29, 2007 10:21 am    Post subject: FilereadLine Reply with quote

Hi,

It seems there is a bug in FilereadLine Command.

I have a text file and I want to create a new file wich contain only line which contain a certain word (%Status%).
The first script (wich use filereadline) didn't stop (loop) if the last line of text file contain the search word.
The second script use a simple loop to read text and it works.

The first script:


Quote:

Status=ERRE

ficsource=c:\aaaaa\TEST_281107_291107.lis

ficstatus=c:\aaaaa\fic_%Status%.lis

Loop
{
FileReadLine, line, %ficsource%, %A_Index%


if vtom=oui
{
ifinstring, line, Visual Tom
{
fileappend, %line%`n, %ficstatus%
fileappend, `n, %ficstatus%
}

ifinstring, line, DATE PASSAGE
fileappend, %line%`n, %ficstatus%

ifinstring, line, ________________
{
fileappend, %line%`n, %ficstatus%
fileappend, `n, %ficstatus%
vtom=non
}
}

ifinstring, line,%A_space%%Status%%A_space%
{


fileappend, %line%`n, %ficstatus%

}

If ErrorLevel
Break

}



exit



The second script:

Quote:

Status=ERRE

ficsource=c:\aaaaa\TEST_281107_291107.lis

ficstatus=c:\aaaaa\fic_%Status%.lis

Loop, read, %ficsource%
{

if vtom=oui
{
ifinstring, A_LoopReadLine, Visual Tom
{
fileappend, %A_LoopReadLine%`n, %ficstatus%
fileappend, `n, %ficstatus%
}

ifinstring, A_LoopReadLine, DATE PASSAGE
fileappend, %A_LoopReadLine%`n, %ficstatus%

ifinstring, A_LoopReadLine, ________________
{
fileappend, %A_LoopReadLine%`n, %ficstatus%
fileappend, `n, %ficstatus%
vtom=non
}
}

ifinstring, A_LoopReadLine,%A_space%%Status%%A_space%
{


fileappend, %A_LoopReadLine%`n, %ficstatus%

}

If ErrorLevel
Break

}



exit



If you need the text file, I can send it.


Thank you for your help.
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2739
Location: Australia, Qld

PostPosted: Thu Nov 29, 2007 11:22 am    Post subject: Re: FilereadLine Reply with quote

I'm not sure exactly how you're expecting FileReadLine to act, but it will not break from the loop automatically, since there's no way for it to know the Loop even relates to file reading. For that, you must check ErrorLevel and break (or not) accordingly. (Assuming ErrorLevel is set when the requested line doesn't exist; I've honestly never used FileReadLine.)
Code:
Loop
{
    FileReadLine, line, %ficsource%, %A_Index%
    if ErrorLevel
        break
    ...

Also read this paragraph from the help file:
FileReadLine wrote:
Generally, this command should be used only for small files, or in cases where only a single line of text is needed. To scan and process a large number of lines (one by one), use a file-reading loop for best performance. To read an entire file into a variable, use FileRead.
Back to top
View user's profile Send private message
motuslechat



Joined: 22 Oct 2004
Posts: 30

PostPosted: Thu Nov 29, 2007 1:15 pm    Post subject: Reply with quote

Thank you for your comment.

My text file is 100 Ko.
Filereadline works fine if the last line doesn't contain the search word.
It make loop only if the last line contain the search word.

You are right about the position of errorlevel. I try to put it just after the filereadline and test.

I'll give you result.
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2739
Location: Australia, Qld

PostPosted: Thu Nov 29, 2007 8:00 pm    Post subject: Reply with quote

My importantly, FileReadLine is much less efficient. It has to open and close the file every iteration, and read through the file to find the relevant line. Loop,Read, on the other hand, opens the file once and reads each line one at a time. It also automatically stops looping when it gets to the end of the file.

I strongly recommend you replace
Code:
Loop
{
    FileReadLine, line, %ficsource%, %A_Index%
    ...
with
Code:
Loop, Read, %ficsource%
{
    line = %A_LoopReadLine%
    ...

Also consider using [code][/code] tags when you post code, as it preserves indentation.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group