Results 1 to 5 of 5

Thread: How Do I Find and Replace....?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Posts
    17

    Question How Do I Find and Replace....?

    Hi, I'm trying to write a piece of code that will look for text thats inputted in my FindString text box in my Source text file and replace it with text in another text box and write it to my target file. How do I go about doing this. I've got this far and now I'm stuck. I know how to copy from one file to another but how do I find and replace.

    Thanks

    Vaastav

    Private Sub cmdReplace_Click()
    Dim sdata As String
    Dim FindString As String
    Dim ReplaceWith As String


    FindString = txtFindString.Text
    ReplaceWith = txtReplaceWith.Text

    Open "c:\Source.txt" For Input As #1
    Open "c:\Target.txt" For Output As #2

    While Not EOF(1)
    Line Input #1, sdata
    Wend

    Print #2, sdata

    Close #1
    Close #2
    End Sub

  2. #2
    Matthew Gates
    Guest
    Use the InStr function to find the text, and if you wish to replace all instances of certain text, you can use the Replace function.


    InStr([start, ]string1, string2[, compare])
    Replace(expression, find, replacewith[, start[, count[, compare]]])

  3. #3
    Matthew Gates
    Guest
    So basically, it goes something like this:


    VB Code:
    1. Private Sub cmdReplace_Click()
    2.     Dim sData As String
    3.     Dim FindString As String
    4.     Dim ReplaceWith As String
    5.  
    6.  
    7.     FindString = txtFindString.Text
    8.     ReplaceWith = txtReplaceWith.Text
    9.  
    10.     Open "c:\Source.txt" For Input As #1
    11.         sData = Input(LOF(1), 1)
    12.     Close #1
    13.    
    14.     sData = Replace(sData, FindString, ReplaceWith)
    15.      
    16.     Open "c:\Target.txt" For Output As #2
    17.         Print #2, sData
    18.     Close #2
    19. End Sub

  4. #4
    Matthew Gates
    Guest
    That was a faster method, if you wish to use what you are currently using with Line Input, it can be done like this:


    VB Code:
    1. Private Sub cmdReplace_Click()
    2.     Dim sData As String
    3.     Dim sText As String
    4.     Dim FindString As String
    5.     Dim ReplaceWith As String
    6.  
    7.  
    8.     FindString = txtFindString.Text
    9.     ReplaceWith = txtReplaceWith.Text
    10.  
    11.     Open "c:\Source.txt" For Input As #1
    12.         Do Until EOF(1)
    13.             Line Input #1, sData
    14.             sText = sText & sData & vbCrLf
    15.         Loop
    16.     Close #1
    17.    
    18.     sText = Replace(sText, FindString, ReplaceWith)
    19.      
    20.     Open "c:\Target.txt" For Output As #2
    21.         Print #2, sText
    22.     Close #2
    23. End Sub

    That would be another way. And also, opening a file, storing it in a variable, and closing it is probably much better than opening two files at once and finding/replacing text in one and writing to the other. Just less complicated .

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2001
    Posts
    17

    Talking Thanks Matthew.

    Thanks for your help mate. You couldn't have made it any clearer.

    Vaastav.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width