Question Find Specific Text In Text File

NoIdeas

Active member
Joined
Aug 13, 2011
Messages
25
Location
Sweden
Programming Experience
1-3
Hi!

I have a little question about txt-files...This is the case:

I am going to make a small software for editing particular parts in text files, so you don't have to do it by hand.

In this program, i need to find several words in a text file and retrieve the text or number that is after the the "="-sign, and then replace that info.
Then, I want to use the information it retrieves to add some text in the end of the file.

P.S. There are 36 differet words to search for, and some of them will occur in several places, so i want the data from those words that occur several times in an array.
And the data i want to retrieve is AFTER the searchword and a "=", so i dont want the searchword or the equals-sign included.

Can someone explain to me how to do this?

//NoIdeas
 
Last edited:
You first need to decide whether you want to read the entire file into your app in one go, process the data and then write it all back out again, or else read the file in blocks, probably line by line, and write it out to a temp file. The main consideration, although there may be others, is how much data there is. If the there is a lot of data then your app will require a lot of memory to hold it all at the same time. So, once you make that decision, we can move on.
 
I'd like to read everything first in one go, and then add it.
The files arent especially big, the biggest are at about 4 kb, but most of them are 1 or 2 kb each.

On the other hand, there are about 1800 of these files, so it might be quite slow. But the speed doesn't really matter in this occasion :)


Um, one more question, does it work if you read it from top line and down, so its reading it line by line, cause i would like to make it move on to next text-file when it finds a dot... :)
 
Last edited:
I succeded to solve my problem. I found some code for searching text files, and modified that a bit so it only reads the line where the word's at.
and then I read about strings and their actions, so i succeded to remove some characters from the strings so now it should work fine :D

Case Solved :D

One forum question just... How do you mark a thread as solved?
 
br
How U can do it

i have also this problem..
How show only a Specific text from textfile.txt in Textbox..?
 
Last edited:
Read each line into an array. Like this...

Dim Lines() As String
Lines = Split(ReadTextFileContents("C:\Test.txt"), vbCrLf)

Public Function ReadTextFileContents(FileName As String) As String
ReadTextFileContents = String.Empty
Try
ReadTextFileContents = System.IO.File.ReadAllText(FileName)

Catch e As Exception
'Some type of Error Handler
End Try

End Function
 
Read each line into an array. Like this...

Dim Lines() As String
Lines = Split(ReadTextFileContents("C:\Test.txt"), vbCrLf)

Public Function ReadTextFileContents(FileName As String) As String
ReadTextFileContents = String.Empty
Try
ReadTextFileContents = System.IO.File.ReadAllText(FileName)

Catch e As Exception
'Some type of Error Handler
End Try

End Function
File.ReadAllLines Method
 
Back
Top