Hi guys,

I'm looking for an easier way to remove blank lines from .txt or .dat format files. The current method I'm using is working but is pretty inelegant -

VB Code:
  1. Dim filedata() As String
  2. Dim finaldata() As String
  3.  
  4. Public Function ReadFromFile(ByVal passedfilename As String)
  5. On Error GoTo Err:
  6.  
  7.     Dim arrlen As Integer: arrlen = 1
  8.     Dim count As Integer: count = 1
  9.     Dim incomingtext As String
  10.     Dim overalltext As String: overalltext = ""
  11.  
  12.     'I start by opening the file.  The example file I'm using is full of data like so-
  13.     '"Filedatablahblahblah"
  14.     '<blank line>
  15.     '"Filedatablahblahblah"
  16.     '<blank line>
  17.     '"Filedatablahblahblah"
  18.     '<blank line>
  19.     'etc.
  20.     Open passedfilename For Input As #1
  21.  
  22.         'What I do here is basically read in every line of data in the file
  23.         'to a slot in one of two String arrays.  The idea is that the first
  24.         'array will contain every line in the file, while the second array
  25.         'will ultimately contain only those lines that aren't blank
  26.         Do While Not EOF(1)
  27.        
  28.             'Read a line from the file
  29.             Line Input #1, incomingtext
  30.             'Keep a record of every input
  31.             overalltext = overalltext & incomingtext
  32.             'Resize the array
  33.             ReDim Preserve filedata(count)
  34.             'Input the current line of text in the file to the array
  35.             filedata(count) = incomingtext
  36.             'Increment the pointer
  37.             count = count + 1
  38.        
  39.         Loop
  40.    
  41.  
  42.     'Close the file
  43.     Close #1
  44.    
  45.     'This is the second array.  This should only contain non-blank lines
  46.     'once processing has finished
  47.     ReDim finaldata(UBound(filedata)) As String
  48.    
  49.     Dim pointer As Integer: pointer = 1
  50.     Dim i As Integer: i = 0
  51.    
  52.     'For every item in the first array
  53.     For i = 0 To UBound(filedata)
  54.    
  55.         'If the item is blank, do not write to the second array
  56.         If filedata(i) = "" Then
  57.        
  58.             filedata(i) = filedata(i)
  59.        
  60.         Else
  61.        
  62.             'If it isn't blank, resize the second array and input the item
  63.             ReDim Preserve finaldata(pointer)
  64.             finaldata(pointer - 1) = filedata(i)
  65.             pointer = pointer + 1
  66.        
  67.         End If
  68.    
  69. Next
  70.    
  71.  
  72. For i = 0 To UBound(finaldata)
  73.    
  74.         MsgBox finaldata(i)
  75.    
  76.     Next
  77.  
  78. Exit Function
  79. Err:
  80.     MsgBox Err.Description
  81.     Exit Function
  82. End Function

The above code is working fine, but I was wondering if there was an easier solution to removing blank lines from a file than the above. (I'm aware that the code I posted won't remove the lines from the file istelf - I intend to write the values of the second array into the file at a later point in the process).

If anyone could help, that would be appreciated.

Many thanks.