• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 883
  • Last Modified:

vb.net remove first line in a text file

I have a text file:

asdfsdfdfdf

john mike      bob       larry
sue     linda    sherry    julie     bobby   mary             cassi

I remove the spaces and replace with commas per code below (Thanks Paul).  I also need to remove the gibbersh on the first line and the 2nd blank line.
0
bergertime
Asked:
bergertime
  • 2
  • 2
1 Solution
 
bergertimeAuthor Commented:
I need to amend my question.  I need to remove every line that starts with *****
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.Net?

...and do you want to:
(1) Copy the file into memory.  Modify the contents in memory.  Replace the entire original file with the new contents.
(2) Use a secondary file.  Read original file line by line.  Write the desired lines to the secondary file.  Delete the original file.  Rename the secondary to the original files name.
0
 
bergertimeAuthor Commented:
2005, how bout option 2
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Haven't tested it yet but something like this should work:
Imports System.IO
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sourceFile As String = "c:\someFile.txt"
        Dim tempFile As String = My.Computer.FileSystem.GetTempFileName
 
        Dim sr As New StreamReader(sourceFile)
        Dim sw As New StreamWriter(tempFile)
 
        Dim line As String = sr.ReadLine
        While Not IsNothing(line)
            If Not line.StartsWith("*****") Then
                sw.WriteLine(line)
            End If
            line = sr.ReadLine
        End While
        sw.Close()
        sr.Close()
 
        My.Computer.FileSystem.DeleteFile(sourceFile)
        My.Computer.FileSystem.RenameFile(tempFile, sourceFile)
    End Sub
 
End Class

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now