Jump to content

Read file backwards


Def
 Share

Recommended Posts

Is there an easier way, than the code below, to read a file backwards?

C:\log.txt

1 - This is line one.
2 - This is line two.
3 - This is line three.
4 - This is line four.
5 - This is line five.
6 - This is line six.
7 - This is line seven.
8 - This is line eight.
9 - This is line nine.
10 - This is line ten.

readfilebackwards.au3

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

$LineCount = 0

;Get line count
While 1
  $line = FileReadLine($file)
  If @error = -1 Then ExitLoop
  $LineCount = $LineCount + 1;is there a ++ in AutoIt?
WEnd

;Read File Backwards by specifying decrementing $LineCount variable
While 1
  $line = FileReadLine($file, $LineCount)
  If @error = -1 Then ExitLoop
  If $LineCount < 1 Then ExitLoop
  MsgBox(0, "This is line " & $LineCount, "Line in file: " & $line, 2)
  $LineCount = $LineCount - 1
WEnd

Def

Link to comment
Share on other sites

  • Developers

Is there an easier way, than the code below, to read a file backwards?

C:\log.txt

Def

<{POST_SNAPBACK}>

I would do it this way... didn't test it but shouldbe close:

Dim $Records
If _FileRead2Array("c:\log.txt", $Records) Then
   For $x = $Records[0] To 1 Step - 1
      MsgBox(0, "This is line " & $x, "Line in file: " & $Records[$x], 2)
   Next
Else
  ; problem reading the file
EndIf
Exit
; Standard UDF available in File.au3: read file to array 
Func _FileRead2Array($SFilepath, ByRef $AArray)
   Local $HFile
   $HFile = FileOpen($SFilepath, 0)
   If $HFile = -1 Then
      SetError(1)
      Return 0
   EndIf
   $AArray = StringSplit( StringStripCR( FileRead($HFile, _
         FileGetSize($SFilepath))), @LF)
   FileClose($HFile)
   Return 1
EndFunc  ;==>_FileRead2Array

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

That's hot, thanks JdeB.

One quick question; I saw in the help file that the maximum # of elements in an array is "16 million" which I imagine is exactly 16777216 (just a guess), so if I was dealing with a file with more than 2^24 lines I would have to go with incrementing an int for each line, then FileReadLine'ing backwards?

I really doubt this log file I'm working with will ever top 16 mill. but was just curious.

I guess I could rotate the file, with AutoIt, "If $records[0] > 2^24".

Def

Edited by Def
Link to comment
Share on other sites

That is a good solution but I don't know why you read the file into the array when it can all be done without wasting the memory space. I would do it this way:

$linecount = _FileCountLines( "c:\log.txt" )

$file = FileOpen( "c:\log.txt", 0 )

For $i = $linecount to 1 Step -1
   $strFileLine = FileReadLine( $file, $i )
   MsgBox( 0, "", "Line Number: " & $i & @CRLF & "Line Contents: " & $strFileLine )
Next

FileClose( $file )

_FileCountLines comes from File.au3. This won't have the array limitations that you will run into with the other code. I haven't tested it but there is no reason it wouldn't work. Hope this helps!

*** Matt @ MPCS

Link to comment
Share on other sites

That is a good solution but I don't know why you read the file into the array when it can all be done without wasting the memory space.

Good point.

$linecount = _FileCountLines( "c:\log.txt" )

That's good stuff.

Thanks Matt.

Def

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