4 Replies - 61181 Views - Last Post: 01 March 2011 - 01:29 AM Rate Topic: -----

#1 Guest_NIXZ*


Reputation:

Code for "Delete a line from text file"

Posted 02 September 2008 - 04:56 AM

Can anyone plz tell me where what is the code for "Delete a line from text file" command?
or a website to look for it?Searched the delete line but i only got VB6 Code.
Is This A Good Question/Topic? 0

Replies To: Code for "Delete a line from text file"

#2 dineeshd  Icon User is offline

  • member icon

Reputation: 38
  • View blog
  • Posts: 619
  • Joined: 30-June 08

Re: Code for "Delete a line from text file"

Posted 02 September 2008 - 05:11 AM

Check this thread...

Removing a line of text from a .txt file
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Code for "Delete a line from text file"

Posted 02 September 2008 - 05:13 AM

View PostNIXZ, on 2 Sep, 2008 - 12:56 PM, said:

Can anyone plz tell me where what is the code for "Delete a line from text file" command?
or a website to look for it?Searched the delete line but i only got VB6 Code.


The other topic went down hill because some people (no names) hadn't read the post fully.
The problem arose cause you are trying to use VB6 / VBScript code in a VB.Net project, they are total different programming languages. Just the remember that not all the vb6 & vbscript code snippets out there on the web are 100% compatible with VB.Net.

So here a way of achieving that capability.
Note: The first line in the in file is 0 (Zero) and not 1 (One)
   Public Sub DeleteLine(ByRef FileAddress As String, ByRef line As Integer)
        Dim TheFileLines As New List(Of String)
        TheFileLines.AddRange(System.IO.File.ReadAllLines(FileAddress))
        ' if line is beyond end of list the exit sub
        If line >= TheFileLines.Count Then Exit Sub
        TheFileLines.RemoveAt(line)
        System.IO.File.WriteAllLines(FileAddress, TheFileLines.ToArray)
    End Sub


Was This Post Helpful? 1

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Code for "Delete a line from text file"

Posted 02 September 2008 - 05:52 AM

To get the original snippet you copied from to work.
First you need to add a reference to Microsoft Scripting Runtime
Project>Add Reference->COM Tab->Microsoft Scripting Runtime->OK

Then alter the code slightly.
 Public Function DeleteLine(ByVal fName As String, ByVal LineNumber As Long) _
	 As Boolean
		'Purpose: Deletes a Line from a text file

		'Parameters: fName = FullPath to File
		'			LineNumber = LineToDelete

		'Returns:	True if Successful, false otherwise

		'Requires:   Reference to Microsoft Scripting Runtime

		'Example: DeleteLine("C:\Myfile.txt", 3)
		'		   Deletes third line of Myfile.txt
		'______________________________________________________________


		Dim oFSO As New Scripting.FileSystemObject
		Dim oFSTR As Scripting.TextStream
		Dim ret As Long
		Dim lCtr As Long
		Dim sTemp As String, sLine As String
		Dim bLineFound As Boolean

		On Error GoTo ErrorHandler
		If oFSO.FileExists(fName) Then
			oFSTR = oFSO.OpenTextFile(fName)
			lCtr = 1
			Do While Not oFSTR.AtEndOfStream
				sLine = oFSTR.ReadLine
				If lCtr <> LineNumber Then
					sTemp = sTemp & sLine & vbCrLf
				Else
					bLineFound = True

				End If
				lCtr = lCtr + 1
			Loop

			oFSTR.Close()
			oFSTR = oFSO.CreateTextFile(fName, True)
			oFSTR.Write(sTemp)

			DeleteLine = bLineFound
		End If


ErrorHandler:
		On Error Resume Next
		oFSTR.Close()
		oFSTR = Nothing
		oFSO = Nothing

	End Function
End Class


Personally i prefer my original solution to using this method, it a lot less code to go wrong. It introduces several bad practices.
Was This Post Helpful? 0
  • +
  • -

#5 manticohd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-March 11

Re: Code for "Delete a line from text file"

Posted 01 March 2011 - 01:29 AM


 Public Shared Sub RemoveAtLine(ByVal filePath As String, Optional ByVal lineRemove As Integer = -1)
            If (String.IsNullOrEmpty(filePath)) Then
                Return
            End If

            Dim lines As New List(Of String)(File.ReadAllLines(filePath))
            If (lineRemove >= 0 And lineRemove < lines.Count) Then
                lines.RemoveAt(lineRemove )
            End If

            File.WriteAllLines(filePath, lines.ToArray())
        End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1