I have as txt file that has blank lines. I need to remove the blank lines the function I made:
Function DeleteBlankLines()
Dim blkLines() As String
Dim fileName As String = Me.txtLoadFile.Text
Try
Dim sd As New StreamReader(fileName)
Dim fileText As String = sd.ReadToEnd()
sd.Close()
blkLines = Split(fileText, vbCrLf)
'Return
'this path is for testing
Dim fs As New FileStream("c:\test1.txt", FileMode.Create)
Dim sw As New StreamWriter(fs)
Dim foundBlank As Boolean
Dim blkLineCtr As Integer = 0
For Each line As String In blkLines
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
blkLineCtr = blkLineCtr + 1
End If
End If
Next
If blkLineCtr > 0 Then
File.Delete("c:\test1.txt")
DeleteBlankLines()
End If
blkLineCtr = 0
sw.Close()
fs.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
Here is the code that calls that function:
Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
Dim EID, EID_2 As String
Dim GID, GID_2 As String
Dim IsParent, IsParent_2 As String
Dim MD5, MD5_2 As String
Dim lineString As String = ""
Dim lineString2 As String = ""
Dim dlmtr As String = ","
Dim fields() As String
Dim fields2() As String
Dim sr As StreamReader
Dim rdr As StreamReader
Dim MD5_Log As StreamWriter
Dim error_log As StreamWriter
Dim loadFile As String = Me.txtLoadFile.Text
Dim folder As String = Me.txtOutputFldr.Text
'Dim temp As String
If Me.txtLoadFile.Text <> "" And Me.txtOutputFldr.Text <> "" Then
Try
DeleteBlankLines()
sr = New StreamReader(loadFile)
MD5_Log = New StreamWriter(folder & "\" & "MD5_log.txt", False)
error_log = New StreamWriter(folder & "\" & "error_log.txt", False)
MD5_Log.AutoFlush = True
error_log.AutoFlush = True
Me.StatusStrip1.Text = "Processing File...................."
While sr.Peek() <> -1
lineString = sr.ReadLine()
If lineString = "" Then
' deal with empty line
Else
fields = lineString.Split(dlmtr)
If fields.Length <> 4 Then
error_log.WriteLine(lineString & "|" & "Error:Field Length <> 4")
End If
EID = fields(0)
GID = fields(1)
IsParent = fields(2)
MD5 = fields(3)
If IsParent <> "YES" Or IsParent = "" Then
error_log.WriteLine("Help")
Else
MD5_Log.WriteLine(EID & "," & GID & "," & IsParent & "," & MD5)
End If
rdr = New StreamReader(loadFile)
While rdr.Peek() <> -1
lineString2 = rdr.ReadLine()
If lineString2 <> "" Then
fields2 = lineString2.Split(dlmtr)
If fields2.Length <> 4 Then
error_log.WriteLine(lineString & "|" & "Error:Field Length <> 4")
GoTo errorskip
End If
lineString2 = rdr.ReadLine()
fields2 = lineString2.Split(dlmtr)
EID_2 = fields2(0)
GID_2 = fields2(1)
IsParent_2 = fields2(2)
MD5_2 = fields2(3)
Else
error_log.WriteLine("Empty Field")
GoTo errorskip
End If
If (IsParent_2 <> "YES") And (GID_2 = GID) And (MD5_2 <> MD5) Then
MD5_Log.WriteLine(EID_2 & "," & GID_2 & "," & IsParent_2 & "," & MD5)
End If
errorskip:
End While
rdr.Close()
rdr = Nothing
End If
End While
'errorskip:
Catch ex As Exception
'System.Diagnostics.Debug.WriteLine(ex.Message)
MessageBox.Show(ex.Message)
Finally
Me.StatusStrip1.Text = "Ready"
MessageBox.Show("Complete")
End Try
Else
If Me.txtLoadFile.Text = "" Then
MessageBox.Show("Please enter the load file")
Me.btnLoadFile.Focus()
ElseIf Me.txtOutputFldr.Text = "" Then
MessageBox.Show("Please select an output directory")
Me.btnOutputFldr.Focus()
End If
End If
End Sub
Is supposed to remove the blank lines. Unfortunatey sometimes there may be as many as 5 blank lines. I have tried everything. Any suggestions or help?
Function DeleteBlankLines()
Dim blkLines() As String
Dim fileName As String = Me.txtLoadFile.Text
Try
Dim sd As New StreamReader(fileName)
Dim fileText As String = sd.ReadToEnd()
sd.Close()
blkLines = Split(fileText, vbCrLf)
'Return
'this path is for testing
Dim fs As New FileStream("c:\test1.txt", FileMode.Create)
Dim sw As New StreamWriter(fs)
Dim foundBlank As Boolean
Dim blkLineCtr As Integer = 0
For Each line As String In blkLines
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
blkLineCtr = blkLineCtr + 1
End If
End If
Next
If blkLineCtr > 0 Then
File.Delete("c:\test1.txt")
DeleteBlankLines()
End If
blkLineCtr = 0
sw.Close()
fs.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
Here is the code that calls that function:
Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
Dim EID, EID_2 As String
Dim GID, GID_2 As String
Dim IsParent, IsParent_2 As String
Dim MD5, MD5_2 As String
Dim lineString As String = ""
Dim lineString2 As String = ""
Dim dlmtr As String = ","
Dim fields() As String
Dim fields2() As String
Dim sr As StreamReader
Dim rdr As StreamReader
Dim MD5_Log As StreamWriter
Dim error_log As StreamWriter
Dim loadFile As String = Me.txtLoadFile.Text
Dim folder As String = Me.txtOutputFldr.Text
'Dim temp As String
If Me.txtLoadFile.Text <> "" And Me.txtOutputFldr.Text <> "" Then
Try
DeleteBlankLines()
sr = New StreamReader(loadFile)
MD5_Log = New StreamWriter(folder & "\" & "MD5_log.txt", False)
error_log = New StreamWriter(folder & "\" & "error_log.txt", False)
MD5_Log.AutoFlush = True
error_log.AutoFlush = True
Me.StatusStrip1.Text = "Processing File...................."
While sr.Peek() <> -1
lineString = sr.ReadLine()
If lineString = "" Then
' deal with empty line
Else
fields = lineString.Split(dlmtr)
If fields.Length <> 4 Then
error_log.WriteLine(lineString & "|" & "Error:Field Length <> 4")
End If
EID = fields(0)
GID = fields(1)
IsParent = fields(2)
MD5 = fields(3)
If IsParent <> "YES" Or IsParent = "" Then
error_log.WriteLine("Help")
Else
MD5_Log.WriteLine(EID & "," & GID & "," & IsParent & "," & MD5)
End If
rdr = New StreamReader(loadFile)
While rdr.Peek() <> -1
lineString2 = rdr.ReadLine()
If lineString2 <> "" Then
fields2 = lineString2.Split(dlmtr)
If fields2.Length <> 4 Then
error_log.WriteLine(lineString & "|" & "Error:Field Length <> 4")
GoTo errorskip
End If
lineString2 = rdr.ReadLine()
fields2 = lineString2.Split(dlmtr)
EID_2 = fields2(0)
GID_2 = fields2(1)
IsParent_2 = fields2(2)
MD5_2 = fields2(3)
Else
error_log.WriteLine("Empty Field")
GoTo errorskip
End If
If (IsParent_2 <> "YES") And (GID_2 = GID) And (MD5_2 <> MD5) Then
MD5_Log.WriteLine(EID_2 & "," & GID_2 & "," & IsParent_2 & "," & MD5)
End If
errorskip:
End While
rdr.Close()
rdr = Nothing
End If
End While
'errorskip:
Catch ex As Exception
'System.Diagnostics.Debug.WriteLine(ex.Message)
MessageBox.Show(ex.Message)
Finally
Me.StatusStrip1.Text = "Ready"
MessageBox.Show("Complete")
End Try
Else
If Me.txtLoadFile.Text = "" Then
MessageBox.Show("Please enter the load file")
Me.btnLoadFile.Focus()
ElseIf Me.txtOutputFldr.Text = "" Then
MessageBox.Show("Please select an output directory")
Me.btnOutputFldr.Focus()
End If
End If
End Sub
Is supposed to remove the blank lines. Unfortunatey sometimes there may be as many as 5 blank lines. I have tried everything. Any suggestions or help?