;========================================================================================================================================== ; How to extract specified line from text file? ; ; Detlev Dalitz.20040327.20090506. ;========================================================================================================================================== ; ; Assuming the given text file is a DOS standard text file ; with each line delimited by a sequence of two bytes @CR@LF, ; we are able to extract a line by the line number. ; There exist different techniques to fetch a line out of a text file. ; ;------------------------------------------------------------------------------------------------------------------------------------------ ; Contents ; ; 1. Using FileRead. ; 2. Using Binary Buffer functions. ; 3. Using Fileget and ItemExtract. ; 4. Using Fileget and ItemExtract (one line code). ; ;------------------------------------------------------------------------------------------------------------------------------------------ ; ; 1. Using FileRead. strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this script as test input file. intLineToExtract = 10 intLine = 0 strLine = "" hdlFR = FileOpen (strFilename, "READ") While @TRUE strLine = FileRead (hdlFR) If strLine == "*EOF*" Then Break intLine = intLine + 1 If intLine == intLineToExtract Then Break EndWhile hdlFR = FileClose (hdlFR) If intLine == intLineToExtract Message (StrCat ("1: This is Line #", intLineToExtract), strLine) Else Message ("Warning", StrCat ("Line not found #", intLineToExtract)) EndIf ;Exit ;------------------------------------------------------------------------------------------------------------------------------------------ ; ; 2. Using Binary Buffer functions. strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this script as test input file. intLineToExtract = 10 intLine = 0 strLine = "" intFilesize = FileSize (strFilename) If !!intFilesize hdlBB = BinaryAlloc (1 + intFilesize) intResult = BinaryPokeStr (hdlBB, 0, @LF) intResult = BinaryReadEx (hdlBB, 1, strFilename, 0, intFilesize) strBBTag = BinaryTagInit (hdlBB, @LF, @CR) While @TRUE strBBTag = BinaryTagFind (strBBTag) If strBBTag == "" Then Break intLine = intLine + 1 If intLine == intLineToExtract strLine = BinaryTagExtr (strBBTag, 0) Break EndIf EndWhile hdlBB = BinaryFree (hdlBB) EndIf If intLine == intLineToExtract Message (StrCat ("2: This is Line #", intLineToExtract), strLine) Else Message ("Warning", StrCat ("Line not found #", intLineToExtract)) EndIf ;Exit ;------------------------------------------------------------------------------------------------------------------------------------------ ; ; 3. Using Fileget and ItemExtract. strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this script as test input file. intLineToExtract = 10 strString = FileGet (strFilename) strString = StrReplace (strString, @CRLF, @LF) If ItemCount (strString, @LF) >= intLineToExtract strLine = ItemExtract (intLineToExtract, strString, @LF) Message (StrCat ("3: This is Line #", intLineToExtract), strLine) Else Message ("Warning", StrCat ("Line not found #", intLineToExtract)) EndIf ;Exit ;------------------------------------------------------------------------------------------------------------------------------------------ ; ; 4. Using Fileget and ItemExtract (one line code). ; ; Almost the same as method 3, but condensed into one code line: strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this script as test input file. intLineToExtract = 10 strLine = ItemExtract (1, ItemExtract (intLineToExtract, FileGet (strFilename), @LF), @CR) Message (StrCat ("4: This is Line #", intLineToExtract), strLine) Exit ;------------------------------------------------------------------------------------------------------------------------------------------ ;==========================================================================================================================================