Read text file after change text " text1 " to " text2" 

Example text file name hhh.txt have content
dsasa asdsa text1 asdksls text1 as

+>>>> hhh.txt
dsasa asdsa text2 asdksls text2 as
Thk :)

Recommended Answers

All 3 Replies

what code have you got, and what part doesn't work right?

You're best bet is to look at the VB samples - Language Samples - String Methods Sample program and get some ideas about how to use the string functions.

There are two methods I use to process text - an array or a buffer.

1) In VB, you can use String.Split to parse it into an array based on a delimiter (default is CrLf). The advantage here is when the text is structured you can get to the part you need using an index number. In you sample, the text you need is in 3rd line, positions 3 and 5. You can get 3rd line with str1=array[2], then parse line by spaces -- Dim myArray() As String = Split(str1, " ") -- and get the 3rd and 5th words -- str2 = myArray[2]; str3 = myArray[4]. This works great if the text is formatted the same every time. Not so good if the format varies.

2) The other option is to read the whole file into a string variable and scan it for the substring 'text1' using Sample.Substring() or Sample.IndexOf().
Tip: Avoid using a temp variable repeatedly to parse strings.

[Note: In VB.Net, StringBuilder can be used to load a long string of text into a buffer and read, modify, replace or delete substrings within the string. It provides lots of flexibility without the performance hit you get with using string functions to read and write to string variables.]

How are you monitoring the file for changes? Do you have an event sink that checks if the file has changed? Do you check the modified date-time at intervals? Do you have a process that fires an event? I'll assume you have a trigger to read the file. If not, you may want to look at the FileSystem samples. You can use the FileSystem object to check the modified date.

Dim fso as new FileSystemObject
Set fso = Nothing
Set fso = New FileSystemObject
strFileName = "D:\Deepak.txt"
fso.OpenTextFile (strFileName)
Set ts = fso.OpenTextFile(strFileName)
While Not ts.AtEndOfStream
    strCurrentLineText = ""
    strCurrentLineText = ts.ReadLine
    Debug.Print strCurrentLineText
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.