Skipping lines in a test file

I routinely work with text files with well over 3200 lines of text. I am trying to write a routine that will parse the file and skip lines (example every 3rd line) and save the new smaller text file.

I am not a college kid trying to get someone to do his homework for him. I am just trying to teach myself to write simple programs that I find useful. I have 5 or 6 VB.Net books and have not seen this topic in any of these books or the net, does anyone have any tips or pointers to get me moving in the right direction?

[809 byte] By [ChuckD1969] at [2007-12-24]
# 1
Here is one way to do this:

Dim i As Integer = 0

Dim sBuf As String = Nothing

Dim fsRead As New StreamReader("BigFile.txt")

Dim fsWrite As New StreamWriter("SmallFile.txt")

While Not fsRead.EndOfStream

i += 1

sBuf = fsRead.ReadLine()

If i Mod 3 = 0 Then

fsWrite.WriteLine(sBuf )

End If

End While

fsWrite.Close()

fsRead.Close()

The Mod operator gets the remainder of a division between i and three.

If we check for a remainder of 0 then we know we have a number

divisible by 3. The net result is that every third line in BigFile.txt

will end up in SmallFile.txt.

hth,

Olice

ocertain at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Here's a simple example routine:

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.

HTH, GL

rkimble at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
Thanks I will give it a try
ChuckD1969 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

there is no way other thatn writing. a file newer. there no update a single line it seems..?

is there any way to update lines or words in a file , other than reading the file and writing the same things.

zybernau at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

I would like to thank everyone for the help. Without forum’s like this trying to learn the basics of VB.NET would be 1,000,000 times harder.

I hope in the future I can absorb enough knowledge to give something back to the forum.

ChuckD1969 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...