Counting Records In Large Files The Easy Way

The other day I needed to be able to tell how many records were in some text files. In .NET, this seems pretty easy -- you can just read in the file and count the lines:

  Dim count = File.ReadAllLines(filename).Length
  

But this can take a long time if the files are really big. And the files I was working with had millions of records.

They also happened to be fixed length records. So all I had to do was find the total size of the file & the length of one record and then divide the former by the latter.

Private Function GetRecordCount(filename As String) As Long
   Dim lineLength = File.ReadLines(filename).First().Length
   lineLength += 2 ' Add 2 bytes for CrLf
   Dim fi = New FileInfo(filename)
   Dim count As Long = fi.Length / lineLength
   Return count
End Function;

Comments

Popular posts from this blog

Restoring Color Icons in Gimp 2.10

My Nullable DateTime Picker

PowerBuilder and SQL Server: Tips On Working Together