How to print certain lines of an input text file to another file in VB - vb.net

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.

Related

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

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.

How to Print a File without using a streamreader in VB.NET

i want to print files like doc,html,xml,...
all examples that i find are using a streamreader
is there something wich prints my file without reading it and save it in a string?
its thery important for me that its dont saving the text in a string first because the files have different encodings and formatting
ty :D

VB.NET Append text to the end of a line in an exiting CSV file

How can I append text to the end a line in a text file with lines already there in VB.NET?
For example my text file looks like this:
header1,header2,header3
value1,value2,value3
stuff1,stuff2,stuff3
and I want to add new text to the end of each line so it looks like this:
header1,header2,header3,newheader
value1,value2,value3,newvalue
stuff1,stuff2,stuff3,newstuff
I know that in Perl you can point to the line and then act on it, is there some VB.net way to do this?
If the file isn't too big, the simplest is, probably, to use the IO.File.ReadAllLines method to read the file into memory and modify the data and write it back with the WriteAllLines method.
For very large files you're probably looking at reading each line modifying it and writing it to a different file.

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.

How to edit a file in VB?

I have a CPP file. I am using VB in VS2005. I have opened that file using the FileSystemObject. I am reading each and every line in that CPP file. I have to comment all the lines until I encounter a return statement. I am using the scripting.textstream to read a line from the CPP file. But I have no idea as to how we can add a // comment to the beginning of every line that I read or even a multiline comment from the beginning till a return statement. Please help.
You seem to be using the FileSystemObject of the Windows script runtime instead of the System.IO.File class's methods. Strange!
The static System.IO.File.ReadAllLines() will read a file (and close it) and return a string array containing all the lines. You could then iterate through the array and add a comment to each line (except if the line starts with return).
Finally, save the changed text to the file using any of the WriteAllLines() method, thus overwriting any contained text.
Adding a multiline comment at the start would be even easier, you would not need to read the lines into an array.