Sign in to follow this  
Followers 0
hamad12

FileRead help!!

6 posts in this topic

#1 ·  Posted (edited)

I've a simple question regarding reading a txt file

I've a file which is basicly a "log file for 1 of the scripts" which is like:

2012-01-29 09:13:05 : start: hamad12 started the script

2012-01-29 09:19:08 : done

etc etc

what am trying to do is to read the first10 chars of the LAST LINE? is it possible and how?

am already able to read the first 10 chart of the FIRST LINE with something like

Local $chars = FileRead($file, 10)

    MsgBox(0, "Char read:", $chars)

can you please help me how to read the last line?

Edited by hamad12

Share this post


Link to post
Share on other sites



Maybe try using function _FileCountLines() to get the total line count. Then use FileReadLine() read the last line. Then use stringleft() to get the first 10 left most characters of that line.

#include <file.au3>
$iTotal = _FileCountLines('log.txt')
$read = FileReadLine('log.txt', $iTotal)
$sFirst_ten = StringLeft($read, 10)

Share this post


Link to post
Share on other sites

If the line number parameter in FileReadLine() is negative then the line count is taken from the end of the file. The example Beege gave can be simplified to:

#include <file.au3>
$sFirst_ten = StringLeft(FileReadLine('log.txt', -1), 10)

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Good call. :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If the line number parameter in FileReadLine() is negative then the line count is taken from the end of the file. The example Beege gave can be simplified to:

#include <file.au3>
$sFirst_ten = StringLeft(FileReadLine('log.txt', -1), 10)

Good call. ;)

thank you both very much!

it's working like a charm :)

silly thing! how to mark the post as solved??

Edited by hamad12

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

silly thing! how to mark the post as solved??

Just go to your first post and click edit, then click "use full editor". The title will be at the top. Just add "<SOLVED>" to the title. I love it when people do this :)

Edited by Beege

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