Hello,
there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:
how do i do this:
I provided a reply in March 2006 to another person with a similar
request, and while I didn't intend to write your entire solution, I have
done just that below (watch for line-wrapping) -
Dim blPossibleEOL As Boolean = False
Dim byTemp As Byte = 0
Dim iFileNum As UInt16 = FreeFile()
Dim sInputFileName As String = "YOUR INPUT FILENAME HERE!!"
Dim sTextLines As String = ""
Dim iCounter As UInt16 = 0
Dim iFileEnd As Integer = FileLen(sInputFileName) - 2
Dim iFileSeekPosition As Integer = iFileEnd
FileOpen(iFileNum, sInputFileName, OpenMode.Binary, OpenAccess.Read)
Do
FileGet(iFileNum, byTemp, iFileSeekPosition)
If blPossibleEOL Then
If byTemp = &HD Then
iCounter += 1
If iCounter = 27 Then 'Change this for whatever number of lines
you are wanting.
Exit Do
Else
blPossibleEOL = False
End If
Else
blPossibleEOL = False
End If
ElseIf byTemp = &HA Then
blPossibleEOL = True
Else
blPossibleEOL = False
End If
iFileSeekPosition -= 1
Loop
sTextLines = StrDup(iFileEnd - (iFileSeekPosition + 1), " ")
FileGet(iFileNum, sTextLines, iFileSeekPosition + 2)
FileClose(iFileNum)
My.Computer.FileSystem.WriteAllText("CUT-LINES OUTPUT FILENAME HERE!!",
sTextLines, False)
Dim srInput As New IO.StreamReader(sInputFileName)
Dim srOutput As New IO.StreamWriter("TRIMMED-LINES OUTPUT FILENAME HERE!!")
iFileEnd = iFileSeekPosition
iFileSeekPosition = 0
Do
sTextLines = srInput.ReadLine
srOutput.WriteLine(sTextLines)
iFileSeekPosition += (Len(sTextLines) + 2)
Loop Until iFileSeekPosition >= iFileEnd
srInput.Close()
srOutput.Close()
'Kill(sInputFileName)
I have tested this with a 200,000 line Text File and the files were
created almost instantly!
Basically, it works like this -
The first loop opens your Input File (Binary Read Mode), and starting
from the end of the file, counts the number of lines you are wanting to
cut. (iCounter = 27 in this case). It does this by counting the CrLf
characters in your text file. This approach has a tremendous speed
advantage as it doesn't have to count ALL of your Input File lines from
the beginning.
Once it has reverse-counted the number of lines you are wanting, it
notes the current file (byte) position, reads the lines from that
position until the end of the file into a string and writes-out those
lines to your new file. You now have a file with just the number of
lines that you required to be cut.
It then opens your Input File in a StreamReader and starts reading line
by line in the second loop. As each line is read, it is written to
another file, which will eventually contain everything except the last
few lines you want cut. It knows when to stop writing by matching the
number of characters read so far (allowing extra 2 bytes for CrLf on
each line) to the original Seek Position obtained earlier.
As already mentioned, this routine is VERY fast, not necessarily pretty,
but fast!
I would encourage you to add error-handling etc., but I trust this gives
you a strong foundation to work from.
ShaneO
There are 10 kinds of people - Those who understand Binary and those who
don't.