Resolved to split text files

andrews

Well-known member
Joined
Nov 22, 2011
Messages
166
Programming Experience
5-10
Dear moderator,
I want to change a text file with lines into a contiguous text.
The text file consists of lines like "15,28,55" but which also end with what I think is something like vbcrl.f
I can't get rid of the latter but this is necessary because I want to convert the entire merge (15,28,55,34,78...) into another text file for making an array by means of split
The code :
Dim reader As StreamReader = File.OpenText("C:\users\AndreAugust\source\repos\balto-zimmermann\allegridtextbestanden\gridtext3.txt")
Dim lijn As String
Dim allelijnen As String
Do While Not reader.EndOfStream
lijn = reader.ReadLine()
lijn.Remove(vbCrLf) '????
allelijnen = allelijnen & lijn
Loop
thanks for any resonse
 
If you have a file like this:
1,2,3
4,5,6
7,8,9
and you want to turn it into a file like this:
1,2,3,4,5,6,7,8,9
then you can do it like this:
VB.NET:
Dim lines = File.ReadLines(inputFilePath)
Dim text = String.Join(",", lines)

File.WeiteAllText(outputFilePath, text)
There are other options but this is simple and will be perfectly fine unless the file is very big. The first line of code creates a list containing the lines from the file. Note that it calls ReadLines rather than ReadAllLines. Both will work but the former is more efficient, so the latter should only be used if required. The second line of code joins the list of lines into a single String with commas between the substrings. The last line then writes that String out to a file.
 
Last edited:
Dear moderator,
It looks great and it is very simple but the last line File.WriteAllText( "C:\users\AndreAugust\source\repos\balto-zimmermann\allegridtextbestanden\gridtext3nw.txt") is not working, there is no file gridtext3nw.txt.
The place for the two items text and path must be changed
Thanks very much!
Regards
 
Back
Top