Reputation: 25
Hi I am looking for a way to take a text file that has 1 set of numbers per line and make a new text file that has taken the set of numbers and put them into 2 spaced out columns per line. Using Visual Basic 2010 Ex:
My First text file will look similar to this: test1.txt
But I would like to convert it to look like this and save it as a new file: text2.txt
With more spacing in between the set of numbers. I have been struggling with this for about a week now and I have put myself back at square one with it. Thank you all for your time and hope you all have a good day. Joe.
Here is my current code. I am embarrassed to say the least. I am taking an exsisting file and formatting it to have a space between each line of numbers and then reading it into a richtextbox to convert it to a barcode font then saving it. At that point I have no idea how to make the format the way I need it to be putting 2 sets of numbers on one line.
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnLoad.Click
ListBox1.Items.AddRange(IO.File.ReadAllLines("C:\test\serintest.txt"))
End Sub
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnConvert.Click
Dim FILE_NAME As String = "C:\test\serscantemp.rtf"
Dim i As Integer
Dim t As Integer
TextBox1.Text = ListBox1.Items.Count.ToString(t)
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
For i = 0 To TextBox1.Text - 1
'objWriter.WriteLine(aryText(i))
objWriter.WriteLine(ListBox1.Items(i))
objWriter.WriteLine("")
Next
objWriter.Close()
Me.RichTextBox.LoadFile("C:\test\serscantemp.rtf", _
RichTextBoxStreamType.PlainText)
RichTextBox.SaveFile("c:\test\barcodetext.rtf", _
RichTextBoxStreamType.RichText)
End Sub
Upvotes: 1
Views: 732
Reputation: 326
Here's another way to do it that you might also want to try though almost the same as Ed's answer. Just call the routine from btnConvert click event. Also make sure to include Import System.IO
Private Sub Reformat(sourceFile As String, destFile As String)
Dim arr As String() = File.ReadAllLines(sourceFile)
Dim s As String = String.Empty
Dim lst As New List(Of String)()
Dim iCtr As Integer = 0
For i As Integer = 0 To arr.Length - 1
If Not String.IsNullOrWhiteSpace(arr(i)) Then
s += arr(i) & " "
iCtr += 1
If iCtr = 2 Then
lst.Add(s.Trim())
iCtr = 0
s = String.Empty
End If
End If
Next
If Not String.IsNullOrWhiteSpace(s.Trim()) Then
lst.Add(s)
End If
File.WriteAllLines(destFile, lst.ToArray())
End Sub
By the way, this is my first post in SO. Just saying.
Upvotes: 0
Reputation: 1693
Joe,
Try this.
I am concatenating the numbers into a string variable and keeping a counter. If the counter = 2 then i write out the concatenated string to RichTextBox and clear counter and string variable
Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
Dim FILE_NAME As String = "C:\temp\serscantemp.rtf"
Dim i As Integer
Dim t As Integer
Dim s As String
Dim c As Integer
TextBox1.Text = ListBox1.Items.Count.ToString(t)
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
c = 0
s = ""
For i = 0 To TextBox1.Text - 1
c = c + 1
If s > "" Then
s = s & " "
End If
s = s & ListBox1.Items(i)
If c = 2 Then
objWriter.WriteLine(s)
objWriter.WriteLine("")
s = ""
c = 0
End If
'objWriter.WriteLine(aryText(i))
Next
objWriter.Close()
RichTextBox1.LoadFile("C:\temp\serscantemp.rtf", _
RichTextBoxStreamType.PlainText)
RichTextBox1.SaveFile("c:\temp\barcodetext.rtf", _
RichTextBoxStreamType.RichText)
End Sub
I hope this is what you are looking for.
Upvotes: 1