שתף באמצעות


Check for empty or blank lines while reading a text file

Question

Sunday, April 16, 2017 8:11 PM

Is there a way to test for an empty or blank line in a text (.txt) file. I am using the following to read the text files

Using MyReader1 As New FileIO.TextFieldParser("C:\BUSRCFiles\VBSRC.txt")
            MyReader1.TextFieldType = FileIO.FieldType.Delimited
            MyReader1.SetDelimiters(Chr(126)) ' is this
            While Not MyReader1.EndOfData
                Try
                    Dim currentRow As String()
                    currentRow = MyReader1.ReadFields()
                    For Each currentField In currentRow
                        'Test for empty or blank line here in the 'currentfield' variable above
                        'if blank or empty, write a single quote " ' " into a new text file
                        'else just write the actual text into the new text file

What happens is it just 'skips' the empty or blank line and keeps going until there is a line with something in it.

Thanks for any help

cyberal

                    

All replies (10)

Monday, April 17, 2017 8:28 AM ✅Answered

Try this approach too:

Using r As New StreamReader("C:\BUSRCFiles\VBSRC.txt")

   While True
      Dim line = r.ReadLine
      If line Is Nothing Then Exit While

      If String.IsNullOrWhiteSpace(line) Then
         ' process empty line
         ' . . .
      Else
         Using sr As New StringReader(line)
            Using MyReader1 As New FileIO.TextFieldParser(sr)
               MyReader1.TextFieldType = FileIO.FieldType.Delimited
               MyReader1.SetDelimiters("~")
               Dim currentRow = MyReader1.ReadFields()
               ' process fields
               ' . . .
            End Using
         End Using
      End If
   End While

End Using

Though it does not support multiline field values.


Sunday, April 16, 2017 8:21 PM

Know what a text file is. 

It is a long string of characters, a line is made by the environment.newline (that is the .Net name). 

But often it contains also a carriage return then it has the name in VB VBCrLF. Both are ASCII characters (that is a code) inside a string. 

Therefore try to think about that if you are working with what you think are lines. It are no lines, the text itself is one long string of unicode characters. 

Be aware that code 126 in ASCII is not the cleverest code. It is the tilde, sometimes as well used, but not so often.

Therefore an empty string is nothing more than twice a new line or twice a carriage return

Success
Cor


Sunday, April 16, 2017 9:19 PM

Cor

I notice that those empty or blank lines I refer to each have a [cr][lf]. Is that something I could test for somehow?

cyberal


Sunday, April 16, 2017 9:25 PM

Of course the simplest way to remove them is 

       theText = TheText.Replace(vbCrLf, "")
        theText = TheText.Replace(Environment.NewLine, "")

Success
Cor


Sunday, April 16, 2017 9:35 PM

Cor,

I'll try to work on this by tomorrow. Looks promising though.

Thanks much

cyberal


Sunday, April 16, 2017 9:53 PM

cyberal,

I used a text file that I put on my desktop. The contents are:

The Rain
In Spain

Lies Mainly
In The

Plain

Using the following it returned 3 & 6 -- it's one-based, not zero-based but it's up to you how you want to do it:

Option Strict On
Option Explicit On
Option Infer Off

Imports System.IO.Path

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load

        Dim desktop As String = _
            Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

        Dim textFilePath As String = _
            Combine(desktop, "ExampleTextFile.txt")

        Dim blankLines() As Integer = GetBlankLines(textFilePath)

        Stop

    End Sub



    Private Function GetBlankLines(ByVal textFilePath As String) As Integer()

        Dim retVal() As Integer = New Integer() {}

        If Not String.IsNullOrWhiteSpace(textFilePath) Then
            If IO.File.Exists(textFilePath) Then
                Dim lineNumber As Integer = 1
                Dim tempList As New List(Of Integer)

                Using rdr As New System.IO.StreamReader(textFilePath)
                    Do While rdr.Peek() >= 0
                        Dim itm As String = rdr.ReadLine.Trim

                        If String.IsNullOrWhiteSpace(itm) Then
                            tempList.Add(lineNumber)
                        End If

                        lineNumber += 1
                    Loop
                End Using

                If tempList.Count > 0 Then
                    retVal = tempList.ToArray
                End If
            End If
        End If

        Return retVal

    End Function
End Class

If that's not what you want then explain more please.

"A problem well stated is a problem half solved.” - Charles F. Kettering


Sunday, April 16, 2017 9:55 PM

Is there a way to test for an empty or blank line in a text (.txt) file. I am using the following to read the text files

The example provided in the class description shows how to use a Try/Catch to determine when rows are invalid and should not be processed, including empty rows.

https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(v=vs.110).aspx


Sunday, April 16, 2017 10:05 PM

Is there a way to test for an empty or blank line in a text (.txt) file. I am using the following to read the text files

The example provided in the class description shows how to use a Try/Catch to determine when rows are invalid and should not be processed, including empty rows.

https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(v=vs.110).aspx

One of us misinterpreted what he's asking.

I thought he was asking "how can I tell if..." and in what I showed, the answer would be based on whether or not the return value is an empty array.

Who knows. ;-)

"A problem well stated is a problem half solved.” - Charles F. Kettering


Sunday, April 16, 2017 11:53 PM

Who knows.

The instruction 'else just write the actual text into the new text file' doesn't match with the use of a textfieldparser, so i's not clear where the inconsistency is.


Tuesday, April 18, 2017 10:03 PM

Thanks so much for everybody's help. I used Viorel's sample to finally solve my question but all suggestions helped!

Thanks again

cyberal