CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2007
    Posts
    38

    replace string in the file

    i want to make a vb application, in which it can read one file named "service.config", when user hit the submit button, it will read this file and if there is string "c:\dev" in that file, it should replace that string to "c:\program".
    Please let me know, it will be great help for me.
    thanks
    abhi

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: replace string in the file

    Here's a simple way to do it.
    Code:
    Private Sub Command1_Click()
      Open App.Path & "\test.txt" For Input As #1
        Do While Not EOF(1)
          Line Input #1, tmpString
          theString = theString + tmpString + vbNewLine
        Loop
      Close #1
    
      theString = Replace(theString, "c:\dev", "c:\program")
      
      Open App.Path & "\test2.txt" For Output As #1
        Print #1, theString
      Close #1
    End Sub
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: replace string in the file

    You were too fast for me, peejavery.
    But since I have done it now, I post it.
    Also this version replaces ALL occurrences of c:\dev. If thats not what you want, abhit, peejavery's Replace is the one to use.
    This here uses FSO
    Code:
    Private Sub ReplaceInFile(FileName As String, sFind As String, sRepl As String)
      Dim FSO As New FileSystemObject
      Dim ts As TextStream
      Dim a$
      
      Set ts = FSO.OpenTextFile(FileName, ForReading)
      If Not ts Is Nothing Then
         a = ts.ReadAll
         ts.Close
         'this replaces all occurrences of sFind with sRepl regardless if it is part of a longer path
         While InStr(1, a, sFind, vbTextCompare): a = Replace(a, sFind, sRepl, , , vbTextCompare): Wend
         Set ts = FSO.OpenTextFile(FileName, ForWriting)
         ts.Write a
         ts.Close
      End If
    End Sub
    
    Private Sub Command1_Click()
      ReplaceInFile App.Path + "\test.txt", "c:\dev", "c:\program"
    End Sub
    I just cant wait for your "skin the cat" version, dglienna.

    I forgot: this version is NOT case sensitive, due to the vbTextCompare options.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: replace string in the file

    Quote Originally Posted by WoF
    Also this version replaces ALL occurrences of c:\dev.
    So does mine, but yours is more professional.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: replace string in the file

    Yes, you are right. The while loop around the Replace statement is not necessary.
    (Dear me )

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: replace string in the file

    We all have those moments!
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Aug 2007
    Posts
    38

    Re: replace string in the file

    Quote Originally Posted by PeejAvery
    We all have those moments!
    thanks WoF and PeejAvery..you both are great..thanks alot...

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: replace string in the file

    Glad we could help. Good luck!
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: replace string in the file

    forgot about this one. hasn't be up in a while...
    Code:
    Option Explicit
    
    ' Add a reference to Riched20.dll.  Browse for it!
    ' This will select TOM.  You only need to do this once!
    '
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const WM_USER = &H400&
    Private Const EM_GETOLEINTERFACE = (WM_USER + 60)
    
    Dim tomDoc As ITextDocument
    
    Private Enum Direction
        Forward = 1
        Backward = -1
    End Enum
    
    Private Sub Command1_Click()
    RTFindText Text1.Text, Forward
    End Sub
    
    Private Sub Command2_Click()
        RTFindText Text1.Text, Backward
    End Sub
    
    ' All the hard work done here
    Private Sub RTFindText(strText As String, Optional RequiredDirection As Direction = Forward)
        Dim flags As Long
        flags = 2 * Check1 + 4 * Check2 ' + 8 * Check3 sadly, RegExp functionality is not available on a RichTextBox
        tomDoc.Selection.FindText Text1.Text, RequiredDirection * tomDoc.Selection.StoryLength, flags
    End Sub
    
    Private Sub Form_Load()
        Dim myIUnknown As IUnknown ' Bet you didn't know that VB actually knows about IUnknown ...
    
        ' Get alternative TOM interface to RTB
        SendMessage RichTextBox1.hwnd, EM_GETOLEINTERFACE, 0&, myIUnknown
        Set tomDoc = myIUnknown ' We're doing a QueryInterface!
        
        ' Ok, set up our RTB with some text
        RichTextBox1.Text = "hello and Hello and hEllo or hello with another hello just for luck saying hello there"
        RichTextBox1.HideSelection = False 'Always show highlight, whether RTB has focus or not
        
        Text1.Text = "Hello" ' Default word to search for
        Command1.Caption = "Find Next"
        Command2.Caption = "Find Previous"
        Check1.Caption = "Whole word"
        Check2.Caption = "Case"
        ' Check3.Caption = "RegExp" 'Sadly RegExp functionality is not available on a RichTextBox
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured