Vb.net What is the best way to parse a txt file that gets updated - vb.net

what I need to accomplish: I need to read a text file that gets updated every 10 sec and dump the new data into. I need to find the best way to do this. I've tried streamreader and file.readalllines, I can get the contents of the txt file, but I don't know how to compare and dump the added lines. Any input would be appreciated, thank you.

Under the assumptions that this is a log file you're reading, where the data is being appended to end, and you're reading the file every 10 seconds, why don't you keep track of the number of lines read or the last line number from the last read?
You can then check to see whether the current read has exceeded the previous line number or number of lines read. If it has exceeded, you should be able to work out what the new lines were added because you know where you read until previously. You can read from the (previous read line + 1) until the end of file.

Related

VBA Excel Reading text file from certain line number onward

say we have an external program, that writes a text file during a long calculation.
Now, I need to read that file in order to check the status of calculation (residuals values, stop on error etc).
The file has size of up to 1Mb. Since the program only adds new information to the end of the file, I dont want to reopen it and reread from the very beginning each time.
Is there a way to start reading the file from a specific line number (where I stopped last time)?
What happens to the file when it is opened in Excel for reading, and after that it is changed by the external program? EoF position shifts forward?
You can use the statement Seek [#]filenumber, position to move to a specific byte in a opened file. Just save the position in the file where you stopped with the function Seek( filenumber ) or with LOF( filennumber ) if you reached the EOF( filenumber ).

Clearing Text on Certain Dates in VB

Ok, so I am working on the very last part of my program and I am wondering if this is possible in VB and how to do it. I was thinking it was probably going to turn into a big If Statement, but I am not sure on the code to be exact.
Basically what I am trying to do is have my program write a new line of text into a .txt file once a day, I got that going good, but now what I want the program to do is to clear the entire .txt file at the start of each month, while also writing to the .txt file after the clear on the same day.
Is this possible? If so, could I get some help on this? Thank you!
Seems to me that it's as simple as this:
If Date.Now.Date.Day = 1 Then
File.Create(Path).Close() ' replace with your path to the file
End If

vb.net writing files of specific size

I am creating an application whereby I need to write files (many, many of them). However, I don't want to write files that are too big because the program that consumes them will crash :)
Essentially, I would like to create files that are no larger than a specific size (possibly 5MB). My data is of CSV format and I will be writing line by line or into some stringbuilder to create the file I need.
Question: Is there a way to write data and create a set of files that are no larger than a set size?
I assume you are creating CSV rows 1 at a time.
If you are using a StreamWriter on the file (hopefully you are, no need to hold it all in memory):
Create the new record (a string of CSV items).
Look at the StreamWriter's BaseStream.Length property (Caveat - You need to use AutoFlush to make that property correct with the data that's been written previously).
Add your stream position, new record's length, and your line separator's length (usually 1 or 2, depending on Cr, Lf, or CrLf), and see if it exceeds your threshold.
If no, write the line feed, the record, and continue. If yes, close your StreamWriter and open a new one for a new file, write the record, and continue.

Opening 90 million lines of text from a .txt file

I am having problems in opening 90 million lines of text from a .txt file and adding them in the array, it is giving me an error and it prevents it from opening. Here is my code:
Dim Contents As String()
Contents = File.ReadAllLines(RichTextBox4.Text)
I have tried 1 million lines of text and it works, but when i tried over 90 million lines it is now giving me an error. Is there any alternative for this? Thanks in advance.
additional info:
These 90 million lines acts as a filter which prevents the user from inputting if the word they input exists in one of the 90 million lines. That is my main goal.
I'd suggest using a steam reader and read a single line each time and then handle that and then read the next line while 'throwing away' the old line.
That way you do not need to read every single line into memory but only the working line.
You can read the huge file in chunks and save each chunk on the disk for later reference.
You can get the underlying file stream and then do something like that:
Dim stream = reader.BaseStream
then use the stream seek method to move the file pointer
stream.Seek(CHUNK_SIZE, SeekOrigin.[Start])

How to print certain lines of an input text file to another file in VB

I have a large file which i am writing to a smaller file in VB, i just dont know how to select lines from an input file that i opened.
I would like to keep the first 12 lines of the large file and them copy every 3rd line into the new output.
Any help?!
You can do this with a StreamReader - just open one on the file, then you can call ReadLine() on it as many times as you need.
Easiest implementation would probably be a 0-to-11 (or 1-to-12) For loop, then a While where you read 2 and ignore them, then read the 3rd and write it.
Writing the new file can be done with StreamWriter, which just has a WriteLine() method to write the text.
The StreamReader and StreamWriter are generally the easiest ways to read and write text files.