Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript remove newline

Tags:

vbscript

I have an HTML page with one or more newlines after </html>. My VBScript file is able to find the replace the newlines with emptiness. But, it looks like OpenTextFile is putting a newline at the end again. Help!

'Pulled this from the InterWebs
Const ForReading = 1 Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("a.html", ForReading)
strText = objFile.ReadAll
'Wscript.Echo strText
objFile.Close

strNewText = Replace(strText, "</html>" & vbCrlf, "</html>")
Set objFile = objFSO.OpenTextFile("a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
like image 462
publicRavi Avatar asked May 23 '26 00:05

publicRavi


2 Answers

Instead of objFile.WriteLine strNewText use objFile.Write strNewText. This will write the file without a newline at the end.

BTW, another way of removing the newline(s) from after your </html> tag would be strNewText = Trim(strText) instead of using Replace()

This may help:

The TextStream object has the following important methods for writing to text files:

  • Write(string) - Writes a string to the open text file.
  • WriteLine(string) - Writes a string to the text file and finishes it off with the new line character.
  • WriteBlankLines(lines) - Writes a specified number of new line characters.

If you do not want a newline at the end, use

objFile.Write strNewText

instead of

objFile.WriteLine strNewText
like image 39
Mike Moe Avatar answered May 24 '26 18:05

Mike Moe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!