user540271
user540271

Reputation: 11

Delete not specific lines in a text file

I'm looking for a way to delete lines that don't meet a criteria in VB.net. I'll just give a example of what I want done below.

Basically, I want the program to go through a text document (Each line) and if the line doesn't contain a certain string it would get erased.

Basically:

hgfhfghhfo TRUE

hdfgdfhdfh MAYBE

tytrteyuet POSSIBLE

ghjfgjgfjf FALSE

That's what the text document would look like, now I want it to not focus on the "RandomInfo" but on the "True/False" If it says TRUE I want it too keep the line. If the line contains anything except True I want it deleted. Can you guys help me with this?

Upvotes: 0

Views: 700

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94645

Try this,

Dim filename = "sample.txt"

Dim result = From n In System.IO.File.ReadAllLines(filename).Where(Function(s) s.EndsWith("TRUE"))

System.IO.File.WriteAllLines(filename, result.ToArray())

Upvotes: 2

Related Questions