Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

VB.NET remove extra blank lines from file

Hello,

I have a file with multiple blank lines in it and I am trying to remove all extra blank lines. Single blank lines need to be preserved but anything more than one needs to be removed so the resulting output is just one blank line between output.

example in:
1

2

3

4

example out:
1

2

3

4

The code I have removes all blank lines.
1
2
3
4

<code>

Imports System
Imports System.IO
Imports System.Collections
Imports System.Text
Imports Microsoft.VisualBasic

Module Module1
Sub Main()

' Create an empty string array to return to caller.
Dim lines() As String = {}

' Check to see if the file exists.
If IO.File.Exists("C:\test.txt") Then

' Open a stream reader to get the text from the file.
Dim sr As New IO.StreamReader("C:\test.txt")

' Read all the file text.
Dim fileText As String = sr.ReadToEnd()

' Split the text into a string array delimited by Carriage Return/Line Feed.
lines = fileText.Split(vbCrLf)

' Close the stream reader
sr.Close()

' Return
Dim sw As New System.IO.Filestream("C:\test1.txt", IO.FileMode.Create)
Dim w as New System.IO.Streamwriter(sw)
Dim i as integer

for i=0 to Ubound(lines)
if lines(i).length > 1 then
w.Write(lines(i))
w.Flush()
end if
next
sw.Close()
End If

End Sub
End Module

</code>

this following vbscript works - but I need it in .NET

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\test.txt", ForReading)

strContents = objFile.ReadAll()
objFile.Close

strNewText = vbCrLf & vbCrLf
strOldText = vbCrLf & vbCrLf & vbCrLf
strOldText1 = vbCrLf & vbCrLf & vbCrLf & vbCrLf
strOldText2 = vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
strOldText3 = vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf

strContents1 = Replace(strContents, strOldText3, strNewText)
strContents2 = Replace(strContents1, strOldText2, strNewText)
strContents3 = Replace(strContents2, strOldText1, strNewText)
strContents = Replace(strContents3, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile("C:\test1.txt", ForWriting)
objFile.Write strContents
objFile.Close

Thanks for any help.

Terry B
[2600 byte] By [terryb] at [2007-11-11 6:53:53]
# 1 Re: VB.NET remove extra blank lines from file
' Create an empty string array to return to caller.
Dim lines() As String = {}

' Check to see if the file exists.
Dim FileName As String = "c:\test.txt"
If File.Exists(FileName) Then
' Open a stream reader to get the text from the file.
Dim sr As New StreamReader(FileName)
' Read all the file text.
Dim fileText As String = sr.ReadToEnd()
' Close the stream reader
sr.Close()

' Split the text into a string array delimited by Carriage Return/Line Feed.
lines = Split(fileText, vbCrLf)

' Return
Dim fs As New FileStream("c:\test1.txt", FileMode.Create)
Dim sw As New StreamWriter(fs)

Dim foundBlank As Boolean
For Each line As String In lines
If Line.Length > 0 Then
sw.WriteLine (Line)
' Reset blank line flag
foundBlank = False
Else
If Not foundBlank Then
' Blank line: write first one
sw.WriteLine (Line)
' Set flag to indicate that blank line was found
foundBlank = True
End If
End If
Next
sw.Close()
fs.Close()
End If
Phil Weber at 2007-11-11 21:50:00 >
# 2 Re: VB.NET remove extra blank lines from file
You have a way of removing all of the blank lines from the array. Pass that array into this subroutine:

'PASS THIS SUBROUTINE YOUR ARRAY OF LINES WITHOUT ANY BLANK LINES
'IN BETWEEN - THIS WILL ADD THE NECESSARY BLANK LINES
Private Sub InsertBlankLines(ByRef passedLines() As String)
Dim s As String = ""
Dim blank As String = " "
Dim count As Integer = passedLines.Length * 2
Dim newLines(count) As String
Dim i As Integer = 0
For Each s In passedLines
newLines(i) = s
newLines(i + 1) = " "
i = i + 2
Next
passedLines = newLines
End Sub

This takes the passed array by reference, creates a new array twice the size as the original, cycles through all of the lines of the array placing blank lines in between the lines with text. By removing all blank lines in the array and then placing your own in you will not have to keep count of which lines are blank or not and/or which may or may not have had blank lines preceeding or following them.

Let me know if this helps...
str_test at 2007-11-11 21:50:55 >