I needed a way of adding periods to the end of every line in text documents. The text documents in question had 100+ lines, so going through each document and pressing ( END > . > ENTER ) repetitively was not ideal. I needed a program, but after some brief googling I gave up and decided that I needed to write this program. I started my project by getting some advice and direction, here, and I am glad I did or else I would have went with my original plan of working with “regex” 🙁 …
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter
Dim Needy As String = tbx_main.Text
Dim Added As String = Regex.Replace(Needy, “(\p{P})(?=\Z|\r\n)”, “.”)
oWrite.Write(Added)
oWrite.Close()
oWrite = oFile.CreateText(“C:\Text_to_Speech.txt”)
End Sub
The end result is a little program that adds any desired character(s) to the beginning or end of all lines pasted into its textbox. The code is as follows…
Try
‘Make sure big tbx is not empty
If tbx_main.Text = “” Then
tbx_main.Text = “Please enter text!”
ElseIf tbx_main.Text = “Please enter text!” Then
tbx_main.Text = “Please enter text!”
Else
‘Make sure small tbx is not empty
If tbx_desired.Text = “” Then
tbx_desired.Text = “Char?”
ElseIf tbx_desired.Text = “Char?” Then
tbx_desired.Text = “Char?”
Else
‘Declare: file, stream and savefiledialog
Dim oFile As File
Dim oMyStream As Stream
Dim saveFileDialog1 As New SaveFileDialog
‘Set savefiledialog defaults
saveFileDialog1.Filter = “txt files (*.txt)|*.txt”
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
‘Run nested code if user presses OK button in dialog
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
oMyStream = saveFileDialog1.OpenFile()
Dim oWrite As New StreamWriter(oMyStream)
For Each LineInput As String In tbx_main.Lines
‘Core Code, add periods to beginning or end
If rbtn_begin.Checked = True Then
‘Beginning
oWrite.WriteLine(tbx_desired.Text + LineInput)
ElseIf rbtn_end.Checked = True Then
‘End
oWrite.WriteLine(LineInput + tbx_desired.Text)
End If
Next
oWrite.Close()
End If
‘Periods added, display success msgbx, close form
MessageBox.Show(“Success: file was created!”)
Me.Dispose()
Me.Close()
End If
End If
Catch exc As Exception
‘Catch Errors, display messagebox
MessageBox.Show(“Error: file was not created!”)
End Try
Sounds useful? If you want to download the program and use it at your own risk, click here.