Example 4 - scan file for lines starting with given text

<< Click to Display Table of Contents >>

Navigation:  Introducing ThinBASIC >

Example 4 - scan file for lines starting with given text

 

This example load and scan an input file searching for lines starting with "TCC".

If any found, outputs a new file containing only such lines.

 

'----------------------------------------------------------------------

 

Uses "File"

 

' Start timer

Ext T1 = TIMER

 

String sInData = File_Load( App_SourcePath + "Test_FileLineSelect(In).TXT" )

 

' This function will parse the input string for $CRLF delimiters

' filling and dimensioning sLines array and returning the number of lines found

String sLines()

Long nLines = Parse sInData, sLines, $CRLF

 

' Now scan the array in order to find whatever string

' If string is found, corresponding line will be appended

' to output buffer

String sOutput

For Count As Long = 1 To nLines  

If LEFT$(sLines(Count), 3) = "TCC" Then

   sOutput += sLines(Count) + $CRLF ' Equivalent of longer sOutput = sOutput + sLines(Count)

End If

Next

 

' Output buffer will be saved to output file

' If file exists it is overwritten

String sOutFile = "Test_FileLineSelect(Out).TXT"

File_Save(App_SourcePath + sOutFile , sOutput)

 

' End timer

Ext T2 = Timer

 

' Show total time

MsgBox 0, "Total time: " + Format$(T2 - T1, "#0.0000") + " sec"