|
-
Jul 12th, 2007, 12:01 AM
#1
Thread Starter
Hyperactive Member
[2005] Text file Manipulation
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
-
Jul 12th, 2007, 12:45 AM
#2
-
Jul 12th, 2007, 02:42 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] Text file Manipulation
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)
ASDADAD13123123
ASDASDASD12312323
ASD123123
ADHDGD
ASD4234234
ASDAAADD
DA
DAD
Total Qty
dasd
524234
sdfsf
456456
fasad
35345345
asdasdad
-
Jul 12th, 2007, 02:48 AM
#4
Re: [2005] Text file Manipulation
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
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jul 12th, 2007, 03:43 AM
#5
Re: [2005] Text file Manipulation
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)
-
Jul 12th, 2007, 03:54 AM
#6
Member
Re: [2005] Text file Manipulation
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".
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]
-
Oct 14th, 2007, 02:34 AM
#7
Frenzied Member
Re: [2005] Text file Manipulation
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
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
|