How to read file and compare with string ?
-
Sunday, July 01, 2012 12:35 PM
Hello everybody
I have a file text , more line
This is my text :
THis is msdn
http://social.msdn.microsoft.com That is msdn http://social.msdn.microsoft.com Here is msdn http://social.msdn.microsoft.com
Now, I want to find "social.msdn.microsoft.com" and delete it
This is result of file after delete
THis is msdn That is msdn Here is msdn
How to do it ? I`m try
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim path As String = "D:\Demo\test" Dim sr As StreamReader = New StreamReader(path, True) Dim sInputLine = sr.ReadLine() Do Until sInputLine Is Nothing sInputLine = sr.ReadLine() If sInputLine = "social.msdn.microsoft.com" Then sInputLine.Remove() End If Loop End Sub
Can you help me . Thank so much.- Edited by headshot9x9 Sunday, July 01, 2012 12:36 PM
All Replies
-
Sunday, July 01, 2012 12:43 PM
Hello headshot9x9,
Hello everybody
I have a file text , more line
This is my text :
THis is msdn
http://social.msdn.microsoft.com That is msdn http://social.msdn.microsoft.com Here is msdn http://social.msdn.microsoft.com
Now, I want to find "social.msdn.microsoft.com" and delete it
This is result of file after delete
THis is msdn That is msdn Here is msdn
How to do it ? I`m try
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim path As String = "D:\Demo\test" Dim sr As StreamReader = New StreamReader(path, True) Dim sInputLine = sr.ReadLine() Do Until sInputLine Is Nothing sInputLine = sr.ReadLine() If sInputLine = "social.msdn.microsoft.com" Then sInputLine.Remove() End If Loop End Sub
Can you help me . Thank so much.use Equals method ,
If sInputLine.Equals("social.msdn.microsoft.com") Then sInputLine.Remove() End IfRegards.
- Carmelo La Monica
- Visual Basic Tips e Tricks Blog
- WordPress.com Blog
- Blogger
-
Sunday, July 01, 2012 12:58 PM
Hi Carmelo La Monica
First , thank you . Then i have two problem :
1) how to delete that line , so that my file only
THis is msdn That is msdn Here is msdn
2)the case file in this text file is not there to do so?
-
Sunday, July 01, 2012 1:03 PM
You can't update a text file like that. You need to write a new file.
Dim path As String = "D:\Demo\test"
Dim Lines As String() = File.ReadAllLines(path)File.Delete(path)
For Each Line As String In Lines
If Line <>"social.msdn.microsoft.com" Then
File.AppendAllText(path, Line & Environment.NewLine)
Next Line- Edited by AcamarMicrosoft Community Contributor Sunday, July 01, 2012 1:04 PM sp
- Proposed As Answer by Reed KimbleMicrosoft Community Contributor, Moderator Sunday, July 01, 2012 4:18 PM
- Marked As Answer by headshot9x9 Monday, July 02, 2012 4:11 AM
-
Sunday, July 01, 2012 1:08 PM
Hi Acamar
Do you think it useful in this case, or do we just delete the line, at which the string found. If the case file is too large, delete and record the work is time consuming
Hope you respond soonfinding your way just in case the search string are written, assuming it is behind a number of other characters do not do it this way
THis is msdn http://social.msdn.microsoft.com That is msdn http://social.msdn.microsoft.com Here is msdn http://social.msdn.microsoft.com
you can try with my purpose
This is result
THis is msdn That is msdn Here is msdn
Thank you
- Edited by headshot9x9 Sunday, July 01, 2012 1:21 PM
-
Sunday, July 01, 2012 1:41 PM
OH, we can try
Dim path As String = "E:\Demo\test" Dim Lines As String() = File.ReadAllLines(path) File.Delete(path) For Each Line As String In Lines If Equals(Line, "google.com.vn") = False Then File.AppendAllText(path, Line & Environment.NewLine) End If Next Line
-
Sunday, July 01, 2012 2:35 PM
I find error in at line
File.Delete(path)
This is show error
The process cannot access the file E:\Demo\test because it is being used by another process
And this is my code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim path As String = "D:\Demo\test" Dim sr As StreamReader = New StreamReader(path, True) Dim Lines As String() = File.ReadAllLines(path) File.Delete(path) For Each Line As String In Lines If Equals(Line, "google.com.vn") = False Then File.AppendAllText(path, Line & Environment.NewLine) End If Next Line End Sub
can you see it -
Sunday, July 01, 2012 2:47 PM
As you don't use the streamreader, you can delete the declaration line.Armin
- Proposed As Answer by Reed KimbleMicrosoft Community Contributor, Moderator Sunday, July 01, 2012 4:18 PM
-
Sunday, July 01, 2012 10:13 PM
I misread your input file format. Change the test to
If Not Line.EndsWith("social.msdn.microsoft.com") Then
to meet your original description. I'm not sure where Google has come from, but you might be suggesting that there is a range of different lines that you want to ignore. For instance, the test might need to be:
If Line.SubString(0,5) <> "http:" Then
It all depends on that file. If there is a range of identifiers then put them all in a list of string and work through the list in a loop - if it gets to the end of the loop without finding a match, write the line.
-
Sunday, July 01, 2012 10:46 PMBTW, I wouldn't delete the original file before the new one has been written.
Armin
-
Sunday, July 01, 2012 11:05 PM
-
Sunday, July 01, 2012 11:26 PM
BTW, I wouldn't delete the original file before the new one has been written.
That would require creating the new file with a unique name then renaming it after deleting the original. That's considerably beyond the original task. However, if that sort of protection is required it's probably simpler to create a backup of the original before deleting it.
-
Sunday, July 01, 2012 11:28 PM
"That's considerably beyond the original task."
Sure. Though I think it's ok to give that hint.
Armin

