;========================================================================================================================================== ; ; How to get the total number of lines in a text file? ; ;------------------------------------------------------------------------------------------------------------------------------------------ ; Detlev Dalitz.20110303. ;========================================================================================================================================== ; Create a temporary text file. intLinesMax = 40 strFilename = ShortCutDir ("Local Settings", 0, 1) : "Temp\" : "WB. " : StrReplace (TimeYmdHms (), ":", "") : ".txt" IntControl (53, 1, 0, 0, 0) ; Set line terminator for FileWrite. p1: 0=None, 1=@CRLF, 2=@LF, 3=@CR, 4=@TAB. hdlFW = FileOpen (strFilename, "WRITE") For intLine = 1 To intLinesMax FileWrite (hdlFW, "Line " : intLine) Next hdlFW = FileClose (hdlFW) Drop (hdlFW, intLine) hdlWinId = WinItemProcId (RunShell (strFilename, "", "", @ZOOMED, @GETPROCID), 0, 0) ;------------------------------------------------------------------------------------------------------------------------------------------ :Test1 ; Use 2x Array functions. intLines1 = ArrInfo (ArrayFileGet (strFilename), 1) :Test2 ; Use 2x File functions and 2x Itemlist functions. intLines2 = ItemCount (FileGet (strFilename), @LF) - (ItemExtract (-1, FileGet (strFilename), @LF) == "") :Test3 ; Use 2x File functions and 2x String functions. intLines3 = FileSize (strFilename) - StrLen (StrReplace (FileGet (strFilename), @LF, "")) :Test4 ; Use 1x File function and 4x Binary functions. #DefineFunction udfGetFileLineCountBB (strFilename) hdlBB = BinaryAlloc (FileSize (strFilename, 1)) intLines = BinaryStrCnt (hdlBB, 0, BinaryRead (hdlBB, strFilename) - 1, @LF) hdlBB = BinaryFree (hdlBB) Return intLines #EndFunction intLines4 = udfGetFileLineCountBB (strFilename) :Test5 ; Use 3x File functions and a while loop. #DefineFunction udfGetFileLineCountFR (strFilename) intLines = 0 hdlFR = FileOpen (strFilename, "READ") While "*EOF*" != FileRead (hdlFR) intLines = intLines + 1 EndWhile hdlFR = FileClose (hdlFR) Return intLines #EndFunction intLines5 = udfGetFileLineCountFR (strFilename) ; Display result. strMsgTitle = "Get total number of lines in a text file." strMsgText = "Test 1, Lines = " : intLines1 strMsgText = strMsgText : @LF : "Test 2, Lines = " : intLines2 strMsgText = strMsgText : @LF : "Test 3, Lines = " : intLines3 strMsgText = strMsgText : @LF : "Test 4, Lines = " : intLines4 strMsgText = strMsgText : @LF : "Test 5, Lines = " : intLines5 Pause (strMsgTitle, strMsgText) :CANCEL ; Delete test file. If IsDefined (hdlWinId) Then If hdlWinId != "" Then If WinExist (hdlWinId) Then WinClose (hdlWinId) If FileExist (strFilename) == 1 Then FileDelete (strFilename) Exit