this what you wanted or did you want to replace the whole line?
VB Code:
'requires System.IO Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strFile As String = InputBox("FilePath?") Dim strSearch As String = InputBox("Search String?") Dim strReplace As String = InputBox("Replace String?") FileSearch(strFile, strSearch, strReplace) End Sub Public Function FileSearch(ByVal strFilePath As String, ByVal strSearch As String, ByVal strReplace As String) Dim strArray() As String = LoadFileToArray(strFilePath) Dim retVal As Integer = SearchAndReplaceArray(strSearch, strReplace, strArray) WriteArrayToFile(strFilePath, strArray) MsgBox(retVal & " lines contained '" & strSearch & "'!") End Function Private Function LoadFileToArray(ByVal strFilePath As String) As String() Dim sr As New StreamReader(strFilePath) Dim strLines() As String = Split(sr.ReadToEnd, vbCrLf) sr.Close() Return strLines End Function Private Function WriteArrayToFile(ByVal strFilePath As String, ByVal strArray() As String) Dim sw As New StreamWriter(strFilePath) Dim I As Integer For I = 0 To strArray.GetUpperBound(0) sw.Write(strArray(I) & vbCrLf) Next sw.Close() End Function Private Function SearchAndReplaceArray(ByVal strSearchFor As String, ByVal strReplaceWith As String, ByRef strArray() As String) As Integer ' Returns how many changed Dim intChanged As Integer Dim I As Integer For I = 0 To strArray.GetUpperBound(0) If InStr(strArray(I), strSearchFor, CompareMethod.Text) <> 0 Then 'Match strArray(I) = Replace(strArray(I), strSearchFor, strReplaceWith) I += 1 End If Next End Function





Reply With Quote