Function: FileReadLines()

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
megnatar
Posts: 92
Joined: 27 Oct 2014, 20:49
Location: The Netherlands
Contact:

Function: FileReadLines()

19 Mar 2019, 12:44

-------------------------------------------------------------------------
FileReadLines
-------------------------------------------------------------------------
This function will read a series of lines from a file, then returns
them to OutVar.

OutVar := FileReadLines( [FromTo, ] FileIn* )

Parameter info.

FromTo:
The range of lines to retrieve from a file.

Parameter can be:
- Empty, then all lines will be retrieved.

- A single number, all lines starting from
that line number will be retrieved.

- Two numbers - a range - divided by a minus character.
Spaces may exist anywhere within the string.
The lower number will always be the starting line.


FileIn:
This 'Thing' that is the input file.

It can be one of the following:
- A quoted path to some file.
- A variable containing file path.
- A file Object, returned by FileOpen().
- It can be empty
, but that would make no sense! :)

-----------------------------------------------------------------------------
------------------------------- EXAMPLES --------------------------------
File := "Q:\dummy.txt"
ReadStart := 22
ReadEnd := 88

ReturnValue := FileReadLines("22 - 88", File)
ReturnValue := FileReadLines(ReadEnd "-" ReadStart, File)

ReturnValue would contain lines 22 through 88 in dummy.txt

-------------------------------------------------------------------------
File := FileOpen("Q:\dummy.txt", 0)
ReadStart := 144

ReturnValue := FileReadLines("144", File)
ReturnValue := FileReadLines(ReadStart, "Q:\dummy.txt")

ReturnValue would contain everything beyond line 143 in dummy.txt

-------------------------------------------------------------------------
ReturnValue := FileReadLines("Q:\dummy.txt")

ReturnValue would contain all lines in dummy.txt.

-------------------------------------------------------------------------
FileReadLines.ahk
(2.63 KiB) Downloaded 83 times
:

Code: Select all

FileReadLines(FromTo=0,FileIn*){
	F:=(!(IsObject(FileIn[1]))?(FileOpen(FileIn[1],0)):F:=FileIn[1]),FileSize:=F.Length(),FileEncodedIn:=F.Encoding(),StartEnd:=StrSplit(FromTo,"-"," `t"),pPoss:="",LinesOut:=""
    if (FileIn[1] = "") ; Show help when calling function with no parameters.
        Return Mbx()
    if (((StartEnd[2])?1:0)&&(StartEnd[1]>StartEnd[2])) ; If there are 2 numbers and the first number is greater then the second one.
		StartEnd.InsertAt(1,StartEnd.pop(StartEnd[2]))  ; Swap places.
    Loop{ ; If there is a ending line. Search starting line, join lines to LinesOut when found. Break loop if end is found. :  If there is no ending line. Search starting line, join lines to LinesOut when found. Break loop if EOF is found.
        if (StartEnd[2])?(((A_Index>=StartEnd[1])?1:!(F.readLine()))&&(((StartEnd[2]-A_Index)=0),LinesOut.=F.readLine())):(((A_Index>=StartEnd[1])?1:!(F.readLine()))&&(((((pPoss:=F.tell())=FileSize))=1),LinesOut.=F.readLine()))
            Break
    }F.Close()
	Return LinesOut
}

Mbx(){
    MsgBox,,Help: FileReadLines, 
    (
    This function will read a series of lines from a file,
    then returns them to OutVar.
        OutVar := FileReadLines( [FromTo, ] FileIn* )

    Parameter info.
    FromTo:
     The range of lines to retrieve from a file.
     Parameter can be:
      - Empty, then all lines will be retrieved.
      - A single number, all lines starting from
        that line number will be retrieved.
      - Two numbers - a range - devided by a minus character.
        Spaces may exist anywhere within the string.
        The lower number will alway's be the starting line.
        
    FileIn:
     This 'Thing' that is the input file.
     It can be one of the following:
      - A quoted path to some file.
      - A variable containing file path.
      - A file Object, returned by FileOpen().
    ----------------------------- EXAMPLES ------------------------------
    File        := "Q:\dummy.txt"
    ReadStart   := 22
    ReadEnd     := 88
    ReturnValue := FileReadLines("22 - 88", File)
    ReturnValue := FileReadLines(ReadEnd "-" ReadStart, File)

    ReturnValue would contain lines 22 through 88 in dummy.txt
    ------------------------------------------------------------------------
    File        := FileOpen("Q:\dummy.txt", 0)
    ReadStart   := 144
    ReturnValue := FileReadLines("144", File)
    ReturnValue := FileReadLines(ReadStart, "Q:\dummy.txt")

    ReturnValue would contain everything beyond line 143 in dummy.txt
    ------------------------------------------------------------------------
    ReturnValue := FileReadLines("Q:\dummy.txt")

    ReturnValue would contain all lines in dummy.txt.
    )
}
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Function: FileReadLines()

19 Apr 2019, 08:20

Nice function. I can use this in some of my applications!
myword
Posts: 3
Joined: 31 Mar 2019, 04:20

Re: Function: FileReadLines()

19 Apr 2019, 11:07

Just wondering what would be a good use case for a partial read of a file?
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Function: FileReadLines()

24 Apr 2019, 06:38

myword wrote:
19 Apr 2019, 11:07
Just wondering what would be a good use case for a partial read of a file?
There are nitche uses. Not something you can use everyday.
User avatar
elModo7
Posts: 217
Joined: 01 Sep 2017, 02:38
Location: Spain
Contact:

Re: Function: FileReadLines()

02 May 2019, 10:58

I could find some uses for this, like having config sections or reading partial code sections from a file.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 157 guests