Connecting Tech Pros Worldwide Forums | Help | Site Map

Deleting A Line From A File

Joe Delphi
Guest
 
Posts: n/a
#1: Nov 21 '05
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.ini", OpenMode.Input)
Do Until EOF(254)
LineOfText = LineInput(254)
FormName = LineOfText.Substring(0, LineOfText.IndexOf("_") -
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.ini 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]
Guest
 
Posts: n/a
#2: Nov 21 '05

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
http://www.mentalis.org/soft/class.qpx?id=6

Ken
------------------
"Joe Delphi" <delphi561@nospam.earthlink.net> wrote in message
news:5jLpe.1399$jX6.220@newsread2.news.pas.earthli nk.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.ini", OpenMode.Input)
Do Until EOF(254)
LineOfText = LineInput(254)
FormName = LineOfText.Substring(0, LineOfText.IndexOf("_") -
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.ini file")
Finally
FileClose(254)
End Try

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

Any help appreciated

JD




Mythran
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Deleting A Line From A File



"Joe Delphi" <delphi561@nospam.earthlink.net> wrote in message
news:5jLpe.1399$jX6.220@newsread2.news.pas.earthli nk.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.ini", OpenMode.Input)
> Do Until EOF(254)
> LineOfText = LineInput(254)
> FormName = LineOfText.Substring(0,
> LineOfText.IndexOf("_") -
> 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.ini 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.OpenText("FormSettings.ini")
Dim contents As String

Try
contents = sr.ReadToEnd()
Catch ex As Exception
MsgBox("Error reading FormSettings.ini 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.CreateText("FormSettings.ini"))

HTH!

Mythran

Cor Ligthert
Guest
 
Posts: n/a
#4: Nov 21 '05

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("TestFile.txt")
Dim sw As New StreamWriter("NewTestFile.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("Mysearch") <> - 1 then
sw.WriteLine(Line)
end if
Loop Until line Is Nothing
sr.Close()
sw.Close()
file.delete("TestFile.txt")
file.move("NewTestFile.txt", "TextFile")
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("There was an error: ")
Console.WriteLine(E.Message)
End Try
End Sub
End Class
///

I hope this helps,

Cor


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Deleting A Line From A File


"Joe Delphi" <delphi561@nospam.earthlink.net> 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.SetLength' 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/>

Cor Ligthert
Guest
 
Posts: n/a
#6: Nov 21 '05

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.SetLength' to reduce the file's length.
>
> --[/color]
How with my sample, can be interresting?

:-)

Cor


Closed Thread