|
-
Jun 17th, 2005, 05:41 AM
#1
Thread Starter
Lively Member
Delete specified line in text file
Another question:
How to delete specified line in text file?
Thanks,
David
-
Jun 17th, 2005, 06:00 AM
#2
Re: Delete specified line in text file
You'll need to read the whole file in, and then write it back out (with the exception of the line you don't want in the file).
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 17th, 2005, 06:01 AM
#3
Thread Starter
Lively Member
Re: Delete specified line in text file
Any other solution? I don't like this
-
Jun 17th, 2005, 06:22 AM
#4
Re: Delete specified line in text file
 Originally Posted by manowar
Any other solution? I don't like this 
You could always give up and move on to another project.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 17th, 2005, 06:24 AM
#5
Thread Starter
Lively Member
Re: Delete specified line in text file
Ok.
Than tell me this. If I load whole text from file. How can I than delete those line ?
-
Jun 17th, 2005, 06:28 AM
#6
Re: Delete specified line in text file
Something like this should do...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim sr As StreamReader
Dim sw As StreamWriter
Dim arrLines As New ArrayList
sr = New StreamReader(New FileStream("c:\somedoc.txt", FileMode.Open))
While sr.Peek <> (-1)
Dim s As String = sr.ReadLine
If s <> SomeCriteriaToDetermineThatItShouldBeDeleted() Then
arrLines.Add(s)
End If
End While
sr.Close()
sw = New StreamWriter(New FileStream("c:\somedoc.txt", FileMode.Create))
For Each s As String In arrLines
sw.WriteLine(s)
Next
sw.Close()
End Sub
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 17th, 2005, 06:47 AM
#7
Thread Starter
Lively Member
Re: Delete specified line in text file
If is there any other solution please let me know please.
-
Jun 17th, 2005, 07:20 AM
#8
Re: Delete specified line in text file
What type of data is in the text file? If it is something that could be stored in an XML file then that might be a better option. .NET provides methods to deal with XML in an OO fashion. Text is just one long string and it's up to you to find the bit you don't want and get rid of it.
-
Jun 18th, 2005, 06:33 AM
#9
Thread Starter
Lively Member
Re: Delete specified line in text file
it normal txt file with text.
-
Jun 18th, 2005, 06:46 AM
#10
Re: Delete specified line in text file
 Originally Posted by manowar
it normal txt file with text.
But what does it represent? Is it just a series of words, like a story or something, or does it have a well defined structure? If it is structured then XML may be a possibility. Otherwise, it's read in, find, delete, resave.
-
Jun 18th, 2005, 03:35 PM
#11
Hyperactive Member
Re: Delete specified line in text file
if you use the find, delete, resave procedure then you might even want to consider the replace() function - might be a little simpler.
Read in the file into one string (i am assuming it's not big - watch out for overflow)
Replace(String, NotNeededStr, ReplaceWithNothing)
Delete file
Write back the whole string to the file
-
Jun 18th, 2005, 04:08 PM
#12
Re: Delete specified line in text file
Why dont you like Crptcblades code example? It does what you asked.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jun 18th, 2005, 05:03 PM
#13
Re: Delete specified line in text file
 Originally Posted by RobDog888
Why dont you like Crptcblades code example? It does what you asked.
They don't like me 'cause I'm black.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 18th, 2005, 05:17 PM
#14
Re: Delete specified line in text file
I thought you were an evil space elf with those pointy ears and yellow Don King hair?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jun 18th, 2005, 05:30 PM
#15
Re: Delete specified line in text file
Only on my mother's side
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jan 25th, 2008, 05:27 PM
#16
Junior Member
Re: Delete specified line in text file
crpt's code, nicely done =]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|