Hi,
I have a text file that has this text in one of the lines - "Total Qty".
I am wondering if it possible to programmatically locate the text and and delete the whole line from the text file.
thanks
Printable View
Hi,
I have a text file that has this text in one of the lines - "Total Qty".
I am wondering if it possible to programmatically locate the text and and delete the whole line from the text file.
thanks
Open the text file into your textbox then replace the text in the textbox and then save it.
use open file dialog to load the text into your textbox1, use code: textbox1.text = textbox1.text.replace(oldtext,newtext) to replace the text, then use WriteAllText to write it back to what ever file you edited.
Edit: oldtext = "Total Qty", newtext = ""
Hello JXDOS,
what you write looks to be pretty logical; only problem is I am not sure if I can code the same..
Wondering if someone cane help
Data in Sample text (c:\test\Data.txt)
Quote:
ASDADAD13123123
ASDASDASD12312323
ASD123123
ADHDGD
ASD4234234
ASDAAADD
DA
DAD
Total Qty
dasd
524234
sdfsf
456456
fasad
35345345
asdasdad
Wouldn't you read it in (think this is fairly easy?) and compare against a removal list. Write it to a new file (use the built in file dialogs?) if it is not in the removal list.
For the future...
If you use a text file of removal items, you could import this into an array then scan your file and create the new one. This means iti s flexible incase you need to add to the list in the future.
pseudocode (easy one):
- open the file to read in
- open the file to write to
-- read in a line - hold in a variable
- compare to the removal list (case statement?)
- if passes write it to the output file
-- Loop until no more lines
- close input file
- close output file
Try doing that in code and post up if you have problems.
You can expand by using a sub with parameters - file to read, file to write
vb Code:
Dim myLines As New List(Of String) Using SR As New IO.StreamReader("c:\test\Data.txt") Dim temp As String = Nothing Do While SR.Peek <> -1 temp = SR.ReadLine If Not temp.Contains("Total Qty") Then myLines.Add(temp) End If Loop End Using IO.File.WriteAllLines("c:\test\Data.txt", myLines.ToArray)
Well, I started writing this out when I realized that you meant removing the whole line, well, here is how you would get rid of that one word:
Some appropriate event(probably after file load into the textbox)
VB.NET Code:
Dim text as string text = textbox.text.replace("Total Qty","")
But, to take out a whole line I would do something like this:
Get your text loaded into a textbox
Make a dynamic array to hold each line
VB.NET Code:
Dim Line() as string Dim Text as string
Set the entire text into a variable
Now loop through the entire text and placing each line into the array "line".VB.NET Code:
text = textbox.text
for each line in text
inside the for loop you could add code to replace the entire line to nothing if it has that word.
Sorry, but that's all I can remember about how I did it last time I had to code something like that. Good search words would be "Visual Basic text parsing".
[Code tags added - Mod]
Iam working on this same function at the moment so i thought id post this example i have found
http://msdn2.microsoft.com/en-us/lib...1y(VS.80).aspx
HTH