Private Sub TrimTextFile(ByVal pathtofile As String)
'ensure the file exists
If System.IO.File.Exists(pathtofile) Then
'read the entire file to memory
'declare a string to hold entire file
Dim entirefile As String
'load the contents from disk
entirefile = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(pathtofile)
'create a string array to hold each line of the file
Dim lines() As String
'split the file into lines
lines = entirefile.Split(ControlChars.NewLine)
'create a stringbuilder to reassemble the desired lines
Dim sb As New System.Text.StringBuilder
'itterate over each line, adding each to the new file, skipping every third
'declare an integer to count lines
Dim linecount As Integer = 1
For Each line As String In lines
If linecount < 3 Then
'append the line to the new file
sb.Append(line.Trim)
sb.Append(ControlChars.NewLine)
'increment the count
linecount += 1
Else
'reset the count, don't add the line
linecount = 1
End If
Next
'save the modified file back over the original
Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText(pathtofile, sb.ToString, False)
End If
There are slicker methods, but this one is drawn out to help show what's happening.