Printing Arranged/Clear/Neat Document in VB - vb.net

I have to print the info that the customer inputs into the program in a special format (see image). I've got it so the customer can enter the information into a database,which they name.I just don't know how to put it in the special format as shown in the image. Do I do it in the code or on the document.
Here is the code:
Imports System.IO
Module Module1
Dim userans As String
Console.WriteLine("Welcome to this program. Choose weather to view the database or add")
userans = Console.ReadLine()
If userans = "add" Then
Dim objStreamwriter As StreamWriter
Console.WriteLine("Please type in the name of your new file")
Dim Myfilename As String = Console.ReadLine()
objStreamwriter = New StreamWriter("C:\Users\Oliver\Documents\Computing\" + Myfilename + ".txt")
Dim TextString = "" 'What you are passing to it.
'Write a line of text to my file defined above
Console.WriteLine("Enter the data. Type Quit at end")
Console.WriteLine("Name ")
Console.Read()
Console.WriteLine("Adress line and number")
Console.Read()
Console.WriteLine("Town")
Console.Read()
Console.WriteLine("Postcode")
Console.Read()
TextString = Console.ReadLine()
While TextString <> "Quit"
objStreamwriter.WriteLine(TextString)
TextString = Console.ReadLine()
End While
'Close the file.
objStreamwriter.Close()
End If
If userans = "view" Then
Dim objStreamReader As StreamReader
Dim strLine As String
Dim filename As String
Console.WriteLine("Please type in the name of the file to view.")
filename = Console.ReadLine()
'Pass the file path and the file name to the StreamReader constructor.
objStreamReader = New StreamReader("C:\Users\Oliver\Documents\Computing\WriteMrFox.txt")
'Read the first line of text.
strLine = objStreamReader.ReadLine
'Continue to read until you reach the end of the file.
Do While Not strLine Is Nothing
'Write the line to the Console window.
Console.WriteLine(strLine)
'Read the next line.
strLine = objStreamReader.ReadLine
Loop
objStreamReader.Close()
End If
End Sub
End Module

Related

How to remove extra line feeds within double quoted fields

Newbie here. Code below removes ALL line feeds in my file but it also removes EOR line feeds. Can somebody please help me how to fix code below so it only removes extra line feeds within double quoted fields? Any help will be greatly appreciated. Thanks
Public Sub Main()
'
Dim objReader As IO.StreamReader
Dim contents As String
objReader = New IO.StreamReader("testfile.csv")
contents = objReader.ReadToEnd()
objReader.Close()
Dim objWriter As New System.IO.StreamWriter("testfile.csv")
MsgBox(contents)
'contents = Replace(contents, vbCr, "")
contents = Replace(contents, vbLf, "")
MsgBox(contents)
objWriter.Write(contents)
objWriter.Close()
'
Dts.TaskResult = ScriptResults.Success
End Sub
I forgot to mention that the input file name changes daily, How do I code so it doesn't care for the file name as long as it as a CSV file? So testfile name has current date and changes daily. I've tried just the file path and it errored out as well. Used the *.csv and it didnt like that either.
objReader = New IO.StreamReader("\FolderA\FolderB\TestFile09212022.csv")
If you are sure there are no double quotes text inside the double quotes you can do it like this:
Dim sNewString As String = ""
Dim s As String
Dim bFirstQuoted As Boolean = False
Dim i As Integer
Dim objWriter As New System.IO.StreamWriter("testfile.csv")
MsgBox(contents)
For i = 1 To contents.Length
s = Mid(contents, i, 1)
If s = """" Then bFirstQuoted = Not bFirstQuoted
If Not bFirstQuoted OrElse (s <> vbLf AndAlso bFirstQuoted) Then
sNewString += s
else
sNewString += " "
End If
Next
MsgBox(sNewString )
objWriter.Write(sNewString )
objWriter.Close()
Dts.TaskResult = ScriptResults.Success

How to read from a file and search for the input of the user on the file? VB.NET

This my code i want to search for the input of the user i.e string on a certain .txt file and if it finds it displays date found else input not found.
Dim freader As IO.StreamReader
Dim strline, a As String
freader = New IO.StreamReader(" C:\Users\neWbie889\Documents\vb\strings.txt")
strline = freader.ReadLine
Do While Not strline Is Nothing
strline = freader.ReadLine()
Loop
Console.WriteLine("enter your string")
a = Console.ReadLine()
If strline = a Then
Console.WriteLine("input found")
ElseIf strline <> a Then
Console.WriteLine("input not found")
End If
freader.Close()
the text file consists of data in this order:
750401 234523
456465 345345
054156 34534
023156 534543
156456 435345
You can read all the text from the file into a string:
Dim allText As String = File.ReadAllText("path to file")
and then check for the string the user gave with Contains method:
If allText.Contains(a) = True Then
Console.WriteLine("input found")
Else
Console.WriteLine("input not found")
End If
Imports System.IO
Module Module1
Sub Main()
Dim a As String
Dim allText As String = File.ReadAllText("C:\Users\KronosXtitan\Documents\vb\ddates.txt")
Console.WriteLine("enter a number")
a = Console.ReadLine()
If allText.Contains(a) = True Then
Console.WriteLine("the number was found")
Else
Console.WriteLine("the number was not found")
End If
End Sub
End Module
thanks to γηράσκω δ' αεί πολλά διδασκόμε for helping me solve this

Strange behavior when Looping through Files

This is driving us crazy, this should work!
When ran (Stepping)
It jumps over the debug.print (No Errors)
Then it hits i += 1
Never stops at the next (break point)
But i=51 !
Any Clues
If CheckBox8.Checked = False Then
Exit Function
Else
Dim fInfo As FileInfo()
Dim i As Integer = 0
Dim dInfo As DirectoryInfo = New DirectoryInfo(spath.ToString)
fInfo = dInfo.GetFiles("*.xml")
Dim sfiles As String()
Dim sFile As String
sfiles = Directory.GetFiles(spath, "*.xml")
For Each sFile In sfiles
Try
Debug.Print(sFile.ToString)
i += 1
Catch ex As Exception
Debug.Print(ex.Message)
End Try
Next
End If
This code appears to work correctly
Imports System.IO
Module Module1
Sub Main()
Dim spath As String
spath = "C:\YOUR_DIRECTORY"
Dim fInfo As FileInfo()
Dim i As Integer = 0
Dim dInfo As DirectoryInfo = New DirectoryInfo(spath.ToString)
Try
fInfo = dInfo.GetFiles("*.xml")
For Each fi In dInfo.GetFiles("*.xml")
Dim file_name As String
file_name = fi.Name
Console.WriteLine(file_name)
i = i + 1
Next
Console.WriteLine("Found:" + i.ToString + " Files")
Catch ex As Exception
Console.Write(ex.Message)
End Try
End Sub
End Module
Note: Debug.Print will print to the "Output Window" in Visual Studio, Not the command line, so..... the Debug.Print statements might not be entirely obvious if you dont have the output window open.

Saving data with a file name of my choice

Very kindly, an intelligent member of stack overflow showed me how to loop with 'Do Until' and generate messages boxes to enable a user to save a file or rename one, if it already exists. However, I'm still hitting a wall. I can't save the ListView data in my for loop, with the file name I have chosen in the Input Box (see code below). Its like I have two separate pieces of code because rtb is saving data in a Rich Text File called Test.txt and saveFile has nothing to do with this! Please help
Code
Dim fileSaved As Boolean
Do Until fileSaved
Dim saveFile As String = InputBox("Enter a file name to save this message")
If saveFile = "" Then Exit Sub
Dim docs As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim filePath As String = IO.Path.Combine(docs, "Visual Studio 2013\Projects", saveFile & ".txt")
fileSaved = True
If My.Computer.FileSystem.FileExists(filePath) Then
Dim msg As String = "File Already Exists. Do You Wish To Overwrite it?"
Dim style As MsgBoxStyle = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Critical
fileSaved = (MsgBox(msg, style, "Warning") = MsgBoxResult.Yes)
End If
Loop
'THIS NEXT bit of code saves content to Test.txt NOT saveFile as desired!
Dim rtb As New RichTextBox
rtb.AppendText("Generation, Num Of Juveniles, Num of Adults, Num of Semiles, Total" & vbNewLine)
For Each saveitem As ListViewItem In ListView1.Items
rtb.AppendText(
saveitem.Text & ", " &
saveitem.SubItems(1).Text & ", " &
saveitem.SubItems(2).Text & ", " &
saveitem.SubItems(3).Text & ", " &
saveitem.SubItems(4).Text & vbNewLine)
Next
rtb.SaveFile("C:\Users\SMITH\Documents\Visual Studio 2013\Projects\Test.txt", _
RichTextBoxStreamType.PlainText)
When you try to save the file with the RichTextBox method SaveFile you need to be able to use the variable filePath that receive the input from your user. But this variable is declared inside the block Do Until .... Loop and according to scope rules of variables in VB.NET is not available outside that block. You could move the declaration of the variable before entering the loop
Dim fileSaved As Boolean
Dim filePath As String
Do Until fileSaved
Dim saveFile As String = InputBox("Enter a file name to save this message")
If saveFile = "" Then Exit Sub
Dim docs as String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
filePath = IO.Path.Combine(docs, "Visual Studio 2013\Projects", saveFile & ".txt")
fileSaved = True
If My.Computer.FileSystem.FileExists(filePath) Then
Dim msg As String = "File Already Exists. Do You Wish To Overwrite it?"
Dim style As MsgBoxStyle = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Critical
fileSaved = (MsgBox(msg, style, "Warning") = MsgBoxResult.Yes)
End If
Loop
' The remainder of your code can be left unchanged until the SaveFile line
Now you could use it in the call
rtb.SaveFile(filePath, RichTextBoxStreamType.PlainText)

Loop through the lines of a text file in VB.NET

I have a text file with some lines of text in it.
I want to loop through each line until an item that I want is found*, then display it on the screen, in the form of a label.
*I am searching for the item through a textbox.
That is, in sudo:
For i = 0 To number of lines in text file
If txtsearch.text = row(i).text Then
lbl1.text = row(i).text
Next i
You can use the File.ReadLines Method in order to iterate throughout your file, one line at a time. Here is a simple example:
Dim Term As String = "Your term"
For Each Line As String In File.ReadLines("Your file path")
If Line.Contains(Term) = True Then
' Do something...Print the line
Exit For
End If
Next
Here's a function that will spit back your string from the row that contains your search term...
Public Shared Function SearchFile(ByVal strFilePath As String, ByVal strSearchTerm As String) As String
Dim sr As StreamReader = New StreamReader(strFilePath)
Dim strLine As String = String.Empty
Try
Do While sr.Peek() >= 0
strLine = String.Empty
strLine = sr.ReadLine
If strLine.Contains(strSearchTerm) Then
sr.Close()
Exit Do
End If
Loop
Return strLine
Catch ex As Exception
Return String.Empty
End Try
End Function
To use the function you can do this...
Dim strText As String = SearchFile(FileName, SearchTerm)
If strText <> String.Empty Then
Label1.Text = strText
End If
LOOPING AND GETTING ALL XML FILES FROM DIRECTORY IF WE WANT TEXTFILES PUT "*.txt" IN THE PLACE OF "*xml"
Dim Directory As New IO.DirectoryInfo(Path)
Dim allFiles As IO.FileInfo() = Directory.GetFiles("*.xml")
allFiles = allFiles.OrderByDescending(Function(x) x.FullName).ToArray()
Dim singleFile As IO.FileInfo
For Each singleFile In allFiles
'ds.ReadXml(singleFile)
xd.Load(singleFile.FullName)
Dim nodes As XmlNodeList = xd.DocumentElement.SelectNodes("/ORDER/ORDER_HEADER")
Dim ORDER_NO As String = " "
For Each node As XmlNode In nodes
If Not node.SelectSingleNode("ORDER_NO") Is Nothing Then
ORDER_NO = node.SelectSingleNode("ORDER_NO").InnerText
End If
Next
Next