I have an HTML page with one or more newlines after </html>
. My VBScript file is able to find the replace the newlines with em开发者_如何学运维ptiness. 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
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
Your code is mostly correct. It's not OpenTextFile that's adding a newline, it's WriteLine. If you replace that with Write, it will work as you expect.
精彩评论