Jump to content

FileReadLine - trim or start/stop


JLC123
 Share

Recommended Posts

This should be simple, but for some reason my brain isn't grasping the obvious here.

I have a simple text file(s) - file sizes will always vary, but I will always need to start reading them at the 8th line and stop reading them 2 lines from the bottom

I've tried string trimming but the first 7 lines are not always the same length - I need to kill the lines completely.

I have been able to trim the last 2 lines, but since I'm hoping to kill the first ones, why not kill the last ones while I'm at it?

Also, if it's possible I would like to do this without UDFs, since I downloaded 3.2.8.1 yesterday I can't seem to get those working.

Two wrongs don't make a right, but three lefts do

Link to comment
Share on other sites

  • Moderators

#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 = 8 to $aRecords[0] - 2;Starts on 8th line and doesn't read last two lines
    Msgbox(0,'Record:' & $x, $aRecords[$x])
Next

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.

Link to comment
Share on other sites

Thanks, but as I said - UDFs aren't working since going to 3.2.8.1

"C:\Program Files\AutoIt3\Include\file.zu3(101,57) : ERROR: SetError () [built-in] called with the wrong number of args."

Two wrongs don't make a right, but three lefts do

Link to comment
Share on other sites

Without UDF's

;~ Myfile.txt:
;~ Line 1
;~ Line 2
;~ Line 3
;~ Line 4
;~ Line 5
;~ Line 6
;~ Line 7
;~ Line 8
;~ Line 9
;~ Line 10
;~ Line 11
;~ Line 12
;~ Line 13
$Line=1
Do  
    $ReadLine=FileReadLine("myfile.txt",$line)
    If $ReadLine<>"" Then 
        $Line+=1
        ConsoleWrite("line read: "&$ReadLine&@cr)
    EndIf
Until $ReadLine=""
ConsoleWrite("->Number of lines: " & $Line -1& @CRLF)
ConsoleWrite("->Should stop reading at: " & $Line-3 & @CRLF)
ConsoleWrite(">Reading lines.. "& @CRLF)
For $x=8 To $Line-3
    $ReadLine=FileReadLine("myfile.txt",$x)
    ConsoleWrite($ReadLine & @CRLF)
Next
Edited by Nahuel
Link to comment
Share on other sites

  • Moderators

Thanks, but as I said - UDFs aren't working since going to 3.2.8.1

"C:\Program Files\AutoIt3\Include\file.zu3(101,57) : ERROR: SetError () [built-in] called with the wrong number of args."

Huh?

Probably because:

#include <File.zu3>

Should be:

#include <File.au3>

But that should have been a different error all together.

Anyway... FileReadLine() is not the right (smart) method here. You are opening and closing the file many many times, and that causes lack of speed.

I haven't looked at the FileReadToArray() function, but I would imagine it would look like this.

;Reading the file one time is all you need

$aArray = _myFileReadToArray($hFileIn)
If IsArray($aArray) = 0 Then
    MsgBox(16, "Error", "Not an Array")
    Exit
EndIF

ConsoleWrite("There are: " & $aArray[0] & " lines." & @CRLF)

For $iCC = 8 To $aArray[0] - 2;Start on line 8 end do not work the last two lines
    ConsoleWrite($aArray[$iCC], @CRLF)
Next

Func _myFileReadToArray($hFile)
    Local $aSplit = StringSplit(FileRead($hFile), @CRLF, 1)
    Return $aSplit
EndFunc

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.

Link to comment
Share on other sites

The .zu3 was just a typo on my part cuz I didn't cut and paste

ALL UDFs I've tried give a similar error. <shrug>

Speed seems ok - all files I'm reading are really very small. (less than 2,000 bytes)

Two wrongs don't make a right, but three lefts do

Link to comment
Share on other sites

  • Moderators

The .zu3 was just a typo on my part cuz I didn't cut and paste

ALL UDFs I've tried give a similar error. <shrug>

Speed seems ok - all files I'm reading are really very small. (less than 2,000 bytes)

I told myself when I posted that, that you'd say the above only if you didn't understand arrays.

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.

Link to comment
Share on other sites

Thanks, but as I said - UDFs aren't working since going to 3.2.8.1

"C:\Program Files\AutoIt3\Include\file.zu3(101,57) : ERROR: SetError () [built-in] called with the wrong number of args."

The .zu3 was just a typo on my part cuz I didn't cut and paste

ALL UDFs I've tried give a similar error. <shrug>

Speed seems ok - all files I'm reading are really very small. (less than 2,000 bytes)

Older versions of AutoIt do not support more then 1 parameter for SetError(). 3.2.8.1 should support up to 3 parameters for using SetError(). You may want to check the AutoIt version that you are actually using which creates the error. Or perhaps Au3Check is responsible for the error but is differcult to say with little info of the output of the error. Either way, check your installation is correct.

<_<

Link to comment
Share on other sites

I told myself when I posted that, that you'd say the above only if you didn't understand arrays.

Guilty. Several exceptionally good teachers without a trace of smugness or superiority about them have tried in vein to teach me, but sadly the mystery of the array remains elusive.

Two wrongs don't make a right, but three lefts do

Link to comment
Share on other sites

Older versions of AutoIt do not support more then 1 parameter for SetError(). 3.2.8.1 should support up to 3 parameters for using SetError(). You may want to check the AutoIt version that you are actually using which creates the error. Or perhaps Au3Check is responsible for the error but is differcult to say with little info of the output of the error. Either way, check your installation is correct.

<_<

Full error cut and paste:

>"C:\Program Files\AutoIt3\SciTe\CompileAU3\CompileAU3.exe" /run /prod /ErrorStdOut /in "C:\work\tmp.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams

>Running AU3Check...C:\Program Files\AutoIt3\SciTe\Defs\Production\Au3Check\au3check.dat

C:\Program Files\AutoIt3\Include\file.au3(101,57) : ERROR: SetError() [built-in] called with wrong number of args.

If Not FileExists($sPath) Then Return SetError(1, 1, "")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Include\file.au3(102,260) : ERROR: SetError() [built-in] called with wrong number of args.

If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringInStr($sFilter, "|")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Include\file.au3(103,79) : ERROR: SetError() [built-in] called with wrong number of args.

If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Include\file.au3(105,48) : ERROR: SetError() [built-in] called with wrong number of args.

If $hSearch = -1 Then Return SetError(4, 4, "")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\work\tmp.au3 - 4 error(s), 0 warning(s)

>AU3Check Ended with Error(s).

>Running: (3.2.8.1):C:\Program Files\AutoIt3\autoit3.exe "C:\work\tmp.au3"

>AutoIT3.exe ended.

>Exit code: 0 Time: 12.138

If I click on "continue anyway" it works - how odd

(Bensonmum)

Two wrongs don't make a right, but three lefts do

Link to comment
Share on other sites

Please update Scite4AutoIt3 to correct your error from Au3Check. CompileAU3 changed name to AutoIt3Wrapper a long time ago. The logic that Au3Check uses now is quite different to the way it used to work.

<_<

that's ever so much better now - thank you!

Two wrongs don't make a right, but three lefts do

Link to comment
Share on other sites

  • Moderators

Full error cut and paste:

>"C:\Program Files\AutoIt3\SciTe\CompileAU3\CompileAU3.exe" /run /prod /ErrorStdOut /in "C:\work\tmp.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams

>Running AU3Check...C:\Program Files\AutoIt3\SciTe\Defs\Production\Au3Check\au3check.dat

C:\Program Files\AutoIt3\Include\file.au3(101,57) : ERROR: SetError() [built-in] called with wrong number of args.

If Not FileExists($sPath) Then Return SetError(1, 1, "")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Include\file.au3(102,260) : ERROR: SetError() [built-in] called with wrong number of args.

If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringInStr($sFilter, "|")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Include\file.au3(103,79) : ERROR: SetError() [built-in] called with wrong number of args.

If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Include\file.au3(105,48) : ERROR: SetError() [built-in] called with wrong number of args.

If $hSearch = -1 Then Return SetError(4, 4, "")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\work\tmp.au3 - 4 error(s), 0 warning(s)

>AU3Check Ended with Error(s).

>Running: (3.2.8.1):C:\Program Files\AutoIt3\autoit3.exe "C:\work\tmp.au3"

>AutoIT3.exe ended.

>Exit code: 0 Time: 12.138

If I click on "continue anyway" it works - how odd

(Bensonmum)

Hmm... This is out of File.au3?... This line:
If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringInStr($sFilter, "|")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")oÝ÷ Ø*.ÖÞ¶¬jëh×6If StringRegExp($sFilter, "(?s)[\\\/:\>\<\|]") Or StringStripWs($sFilter, 8) = "" Then Return SetError(2, 2, "")

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.

Link to comment
Share on other sites

One more question if you don't mind too terribly.

Now that we have the _FileReadToArray working it occurs to me that rather than doing this:

While 1
            $line = FileReadLine($file)
                If @error = -1 Then ExitLoop
                $SRV = StringTrimRight($line, 104)
                $ACT = StringMid($line, 12, 4)
                $CHK = StringMid($line, 19, 5)
                $VID = StringMid($line, 26, 1)
                $ITM = StringMid($line, 33, 22)
                $DTE = StringMid($line, 56, 11)
                $STT = StringMid($line, 67, 8)
                $SEC = StringMid($line, 79, 3)
                $BMP = StringMid($line, 104, 8)
            Wend

I could/should add a demention to the array.

Unfortunately, I can't seem to manage that either. I tried _ArrayCreate but you can guess how that turned out.

Help please!

Two wrongs don't make a right, but three lefts do

Link to comment
Share on other sites

Oops - I was making things over complicated. Sorry

Dim $aRecords
If Not _FileReadToArray("c:\temp\temp.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading file to Array    error:" & @error)
   Exit
EndIf
For $x = 8 to $aRecords[0] - 3;Starts on 8th line and doesn't read last two lines
                $SRV = StringTrimRight($aRecords[$x], 104)
                $ACT = StringMid($aRecords[$x], 12, 4)
                $CHK = StringMid($aRecords[$x], 19, 5)
                $VID = StringMid($aRecords[$x], 26, 1)
                $ITM = StringMid($aRecords[$x], 33, 22)
                $DTE = StringMid($aRecords[$x], 56, 11)
                $STT = StringMid($aRecords[$x], 67, 8)
                $SEC = StringMid($aRecords[$x], 79, 3)
                $BMP = StringMid($aRecords[$x], 104, 8)
                $VAR = IniRead($INI, $CON, $VID, "NULL")
Next

Two wrongs don't make a right, but three lefts do

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