Happy Friday everyone!
I would appreciate some direction on this.
I have processed an .html file as follows:
strStringToClean = Replace(strStringToClean, vbCr, vbCrLf)
strStringToClean = Replace(strStringToClean, vbLf, vbCrLf)
strStringToClean = Replace(strStringToClean, cr, vbCrLf)
strStringToClean = Replace(strStringToClean, lf, vbCrLf)
strStringToClean = Replace(strStringToClean, """""", """")
(as you can see, throwing in some redundant lines in a effort to trouble shoot)
When the file is close and then inspected in NotePad++ I see "CR LF" at the end of every line (the original file has only "LF". However, when I open it for input (Open MyFile for Input as #1), everything is pulled back as a single line, making it almost impossible to parse.
Any thoughts would be greatly appreciated.
Thanks!
Trip
Trying to remember the solution a few days on now... However, if I remember correctly, the issue was that I was writing the output using "Write #1, myString" as opposed to "Print #1, myString". In my experience, "Write" will result in VbCrLf not working properly while "Print" will.
Related
I am relatively new to VBA and want to accomplish something pretty simple but am confused as to why this is not working. I am reading in lines from a file and if opened in notepad++, each line has a CRLF at the end. I would like to just remove the CR. In notepad++ I can do a replaceall, replacing CRLF with LF and things work great. However, the test I have in VBA right now is not doing this correctly. Below is an example of a string I'm dealing with in Notepad++:
Summary - I went for a walk in the parkCRLF
I want the string to become, Summary - I went for a walk in the parkLF
I am writing out to a file as a test in order to see if its working. Below is my code:
Do While Not txtStream.AtEndOfStream
str = txtStream.ReadLine
edited = Replace(str, Chr(13) & Chr(10), Chr(10))
stream.WriteLine (edited)
Loop
txtStream.Close
The code is being executed without error but the CRLF is still at the end of each line in the newly written file...Maybe I'm missing something obviously but the replace does not seem to be picking up on what I'm searching for. Any help or advice on this would be greatly appreciated!
Even figuring out if the end of the line ends with a CRLF would be a step in the right direction at this point.
Thank you.
WriteLine automatically places an end of line after your string
You should replace it with #Mark's suggestion: stream.Write(edited)
This performs better:
stream.Write(Replace(txtStream.ReadAll, vbCrLf, vbLf))
or
stream.Write(Replace(txtStream.ReadAll, vbCr, vbLf))
.
Details about ReadAll
You are reading a line so it will remove the new line characters. Your replace statement has nothing to do. You are then writing a line which will add new line characters.
Also, you aren't closing the output stream in the code sample you provided.
You will want something like this:
Do While Not txtStream.AtEndOfStream
str = txtStream.ReadLine
stream.Write str & Chr(10)
Loop
txtStream.Close
stream.Close
FileOpen(1, filename1, OpenMode.Output)
For index = 0 To 0
PrintLine(1, students_name, correct)
Next
FileClose(1)
End Sub
^^This is the code I am using now, but each time it writes to the file, it erases the data that was there before - I need it to write to the next line in the file instead.
Thanks in advance
Use OpenMode.Append. OpenMode.Output deletes the file contents.
Replace your code with this:
IO.File.AppendAllText(filename1, students_name & vbTab & correct)
Notice how this is suddenly much more readable, and in fact every character now makes sense. Generally, as you convert your legacy code, it should look more concise, easy to read and comprehend.
My VB6 knowledge is rusty, so I had to peek at documentation for what PrintLine with 3 parameters means:
PrintLine(1, "Hello", "World") ' Separate strings with a tab.
Here are two methods for writing text to a file in VB.Net 2012. The first one prepends the same three non-printable characters to each file: . The second one works as expected and does not add the three characters. objDataReader is an OleDB datareader.
Any idea why?
Greg
My.Computer.FileSystem.WriteAllText(lblLocation.Text & "\" &
objDataReader("MessageControlId").ToString & ".txt", objDataReader("MsgContents").ToString, False)
Using outfile As New StreamWriter(lblLocation.Text & "\" & objDataReader("MessageControlId").ToString & ".txt")
outfile.Write(objDataReader("MsgContents").ToString)
End Using
Thanks. I found the entry below I after Googled BOM, in case anyone wants a more detailed explanation. While the BOM was not visible in a text editor it did cause problems when I passed the file to our HL7 interface engine.
Greg
Write text files without Byte Order Mark (BOM)?
I have a form with several text boxes and I want to write the contents of each of them to a new line in a .txt file. As in, the user fills in a form, and the info is stored in the file. Then I want to be able to retrieve the info from the file into the same text boxes. I am able to do this, so far, but I encounter problems when one of the text boxes is multiline.
Printline(1, txtBox1.text)
Printline(1, txtBox2.text)´which is the multiline one
Printline(1, txtBox3.text)
When I read this back from the file I get the second line of the multiline text box where I want the text from txtBox3 to be.
LineInput(1, txtBox1.text)
LineInput(1, txtBox2.text)
LineInput(1, txtBox3.text)
How can I get all the lines from the multiline text box to write to one line in the file, and then read it back as separate lines in a multiline text box?
I hope I am making sense? I really would like to keep the logic of "one txtBox - one line in the file"
I guess I need to use different methods of writing and reading, but I am not that familiar with this, so any help is much appreciated.
You can rely on the Lines Property in case of having more than one line. Sample code (curTextBox is the given TextBox Control):
Using writer As System.IO.StreamWriter = New System.IO.StreamWriter("path", True)
Dim curLine As String = curTextBox.Text
If (curTextBox.Lines.Count > 1) Then
curLine = ""
For Each line As String In curTextBox.Lines
curLine = curLine & " " & line
Next
curLine = curLine.Trim()
End If
writer.WriteLine(curLine)
End Using
NOTE: this code puts in one line all the text from the given TextBox independently upon its number of lines. If it has more than one line, it includes a blank space to separate the individual lines (all of them fitting in a single line of the file anyway). You might want to change this last feature by adding a different separating character (replace & " " & with the one you want).
One option would be to escape the newlines so that they aren't in the output, then unescape them on reading back in.
Here's some example code that will do this (I've never written VB before, so this probably isn't idiomatic):
' To output to a file:
Dim output As String = TextBox2.Text
' Escape all the backslashes and then the vbCrLfs
output = output.Replace("\", "\bk").Replace(vbCrLf, "\crlf")
' Write the data from output to the file
' To read data from the file:
Dim input As String = ' Put the data from the file in input
' Put vbCrLfs back for \crlf, then put \ for \bk
input = input.Replace("\crlf", vbCrLf).Replace("\bk", "\")
' Put the text back in its box
TextBox2.Text = input
Another option would be to store your data in XML, JSON, or YAML. Any of those are text-based formats that will require a library to parse, but should cleanly handle the multiline text you have, along with providing increased future flexibility.
the next simple code works for me.
Saving multiline text to a single line in a file:
str = Replace(MyTextBox.Text, Chr(13) & Chr(10), "*LineFeed*") 'something recognizable
Print #1, str 'no quotes
To get the string from the file and put it on a TextBox:
Line Input #1, str
MyTextBox.Text = Replace(str, "*LineFeed*", Chr(13) & Chr(10))
Hope this helps
I am trying to print a message on a web page in vb.net. I am trying to get the messages in new lines. I tried using the "\r\n" and the new line character. But this is getting printed in the page instead of it comming to the next line. Please let me know if there is any alternative.
Check out Environment.NewLine. As for web pages, break lines with <br> or <p></p> tags.
Environment.NewLine is the most ".NET" way of getting the character, it will also emit a carriage return and line feed on Windows and just a carriage return in Unix if this is a concern for you.
However, you can also use the VB6 style vbCrLf or vbCr, giving a carriage return and line feed or just a carriage return respectively.
The proper way to do this in VB is to use on of the VB constants for newlines. The main three are
vbCrLf = "\r\n"
vbCr = "\r"
vbLf = "\n"
VB by default doesn't allow for any character escape codes in strings which is different than languages like C# and C++ which do. One of the reasons for doing this is ease of use when dealing with file paths.
C++ file path string: "c:\\foo\\bar.txt"
VB file path string: "c:\foo\bar.txt"
C# file path string: C++ way or #"c:\foo\bar.txt"
You need to use HTML on a web page to get line breaks. For example "<br/>" will give you a line break.
If you are using something like this.
Response.Write("Hello \r\n")
Response.Write("World \r\n")
and the output is
Hello\r\nWorld\r\n
Then you are basically looking for something like this
Response.Write("Hello <br/>")
Response.Write("World <br/>")
This will output
Hello
World
you can also just define "<br />" as constant and reuse it
eg.
Public Const HtmlNewLine as string ="<br />"
Response.Write("Hello " & HtmlNewLine)
Response.Write("World " & HtmlNewLine)
it's :
vbnewline
for example
Msgbox ("Fst line" & vbnewline & "second line")
Try Environment.NewLine.
Your need to use the html/xhtml break character:
<br />
you can solve that problem in visual basic .net without concatenating your text, you can use this as a return type of your overloaded Tostring:
System.Text.RegularExpressions.Regex.Unescape(String.format("FirstName:{0} \r\n LastName: {1}", "Nordanne", "Isahac"))
In asp.net for giving new line character in string you should use <br> .
For window base application Environment.NewLine will work fine.
VbCr
Try that.
In this case, I can use vbNewLine, vbCrLf or "\r\n".
vbCrLf is a relic of Visual Basic 6 days. Though it works exactly the same as Environment.NewLine, it has only been kept to make the .NET api feel more familiar to VB6 developers switching.
You can call the String.Replace() function to avoid concatenation of many single string values.
MsgBox ("first line \n second line.".Replace("\n", Environment.NewLine))
Environment.NewLine or vbCrLf or Constants.vbCrLf
More information about VB.NET new line:
http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx
I had the need to store line breaks in a string in a SQL table and have them displayed in vb.NET. My solution was to include a string like this in my database:
"This is the first line{0}This is the second{0}This is the third"
In vb.NET, I processed the string like this before using it:
Label2.Text = String.Format(stringFromSQLquery, vbCrLf)
This replaces every occurance of {0} with vbCrLf