Deleting A Line From A File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Delphi

    #1

    Deleting A Line From A File

    Hi

    Newbie to VB.Net and I have a question

    I need to open a text file, read each line, and if I find something in
    the line, delete that line from the text file. Can anyone tell me how to
    do this. Some of my code is below:

    Try
    FileOpen(254, "FormSettings.i ni", OpenMode.Input)
    Do Until EOF(254)
    LineOfText = LineInput(254)
    FormName = LineOfText.Subs tring(0, LineOfText.Inde xOf("_") -
    1)
    If FormName = Me.Name Then
    (Delete the line from the file somehow......)
    Exit Do
    Else
    FormNameExists = False
    End If
    Loop
    Catch ex As Exception
    MsgBox("Error reading FormSettings.in i file")
    Finally
    FileClose(254)
    End Try

    Also, is there any method in VB.NET for handling .ini files?

    Any help appreciated

    JD



  • Ken Tucker [MVP]

    #2
    Re: Deleting A Line From A File

    Hi,

    You have to read in each line from the file. Recreate the file with
    only the lines you want in the file back to it.

    ini file class
    Free software components for C#, VB.NET and other .NET languages.


    Ken
    ------------------
    "Joe Delphi" <delphi561@nosp am.earthlink.ne t> wrote in message
    news:5jLpe.1399 $jX6.220@newsre ad2.news.pas.ea rthlink.net...
    Hi

    Newbie to VB.Net and I have a question

    I need to open a text file, read each line, and if I find something in
    the line, delete that line from the text file. Can anyone tell me how to
    do this. Some of my code is below:

    Try
    FileOpen(254, "FormSettings.i ni", OpenMode.Input)
    Do Until EOF(254)
    LineOfText = LineInput(254)
    FormName = LineOfText.Subs tring(0, LineOfText.Inde xOf("_") -
    1)
    If FormName = Me.Name Then
    (Delete the line from the file somehow......)
    Exit Do
    Else
    FormNameExists = False
    End If
    Loop
    Catch ex As Exception
    MsgBox("Error reading FormSettings.in i file")
    Finally
    FileClose(254)
    End Try

    Also, is there any method in VB.NET for handling .ini files?

    Any help appreciated

    JD




    Comment

    • Mythran

      #3
      Re: Deleting A Line From A File


      "Joe Delphi" <delphi561@nosp am.earthlink.ne t> wrote in message
      news:5jLpe.1399 $jX6.220@newsre ad2.news.pas.ea rthlink.net...[color=blue]
      > Hi
      >
      > Newbie to VB.Net and I have a question
      >
      > I need to open a text file, read each line, and if I find something in
      > the line, delete that line from the text file. Can anyone tell me how to
      > do this. Some of my code is below:
      >
      > Try
      > FileOpen(254, "FormSettings.i ni", OpenMode.Input)
      > Do Until EOF(254)
      > LineOfText = LineInput(254)
      > FormName = LineOfText.Subs tring(0,
      > LineOfText.Inde xOf("_") -
      > 1)
      > If FormName = Me.Name Then
      > (Delete the line from the file somehow......)
      > Exit Do
      > Else
      > FormNameExists = False
      > End If
      > Loop
      > Catch ex As Exception
      > MsgBox("Error reading FormSettings.in i file")
      > Finally
      > FileClose(254)
      > End Try
      >
      > Also, is there any method in VB.NET for handling .ini files?
      >
      > Any help appreciated
      >
      > JD
      >[/color]

      First off, you should use an arbitrary number for specifying the file
      number. Instead, you should call FreeFile and store the number generated
      from that into a variable and use that var instead. Second off, you should
      use the System.IO namespace for accessing files :) and forget about using
      file numbers completely. That said, let's generate something similar using
      the System.IO namespace:

      <snip>
      Dim sr As StreamReader = IO.File.OpenTex t("FormSettings .ini")
      Dim contents As String

      Try
      contents = sr.ReadToEnd()
      Catch ex As Exception
      MsgBox("Error reading FormSettings.in i file")
      Finally
      sr.Close()
      End Try
      </snip>

      now you can split the contents into an array of strings, a collection,
      ArrayList, whatever. when done modifying whichever method you choose, you
      can then write the contents back to the file, overwriting what is already
      there using a StreamWriter (IO.File.Create Text("FormSetti ngs.ini"))

      HTH!

      Mythran

      Comment

      • Cor Ligthert

        #4
        Re: Deleting A Line From A File

        Joe,

        As alternative

        Roughly changed in this message from this page so watch typos
        http://msdn.microsoft.com/library/de...xtFromFile.asp

        \\\
        Imports System
        Imports System.IO

        Class Test
        Public Shared Sub Main()
        Try
        ' Create an instance of StreamReader to read from a file.
        Dim sr As New StreamReader("T estFile.txt")
        Dim sw As New StreamWriter("N ewTestFile.txt" )
        Dim line As String
        ' Read and display the lines from the file until the end
        ' of the file is reached.
        Do
        line = sr.ReadLine()
        if line.indexof("M ysearch") <> - 1 then
        sw.WriteLine(Li ne)
        end if
        Loop Until line Is Nothing
        sr.Close()
        sw.Close()
        file.delete("Te stFile.txt")
        file.move("NewT estFile.txt", "TextFile")
        Catch E As Exception
        ' Let the user know what went wrong.
        Console.WriteLi ne("There was an error: ")
        Console.WriteLi ne(E.Message)
        End Try
        End Sub
        End Class
        ///

        I hope this helps,

        Cor


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Deleting A Line From A File

          "Joe Delphi" <delphi561@nosp am.earthlink.ne t> schrieb:[color=blue]
          > I need to open a text file, read each line, and if I find something in
          > the line, delete that line from the text file. Can anyone tell me how to
          > do this.[/color]

          In addition to the other replies: I suggest to write the resulting data to
          the original file to prevent the creation timestamp from changing. You can
          use 'FileStream.Set Length' to reduce the file's length.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Cor Ligthert

            #6
            Re: Deleting A Line From A File

            >[color=blue]
            > In addition to the other replies: I suggest to write the resulting data
            > to the original file to prevent the creation timestamp from changing. You
            > can use 'FileStream.Set Length' to reduce the file's length.
            >
            > --[/color]
            How with my sample, can be interresting?

            :-)

            Cor


            Comment

            Working...