Results 1 to 8 of 8

Thread: [RESOLVED] Edit a text file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192

    Resolved [RESOLVED] Edit a text file

    I am trying to edit a textfile:
    Open the textfile, find the item I need and then replace the new item(line) to the file.
    Until now I have got this:

    Code:
    Open "C:\TextFile.txt" For Input As #1 
      While Not EOF(1)
        Line Input #1, strData
        'Found the line to replace  
    Wend
    Close #1
    I hope someone can help me,
    Regards, Kars

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Edit a text file

    So, what exactly is your question? What's the problem with your code?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    Re: Edit a text file

    To find a string you can use this code (in case if you're loading it into rtb)
    Code:
    Dim text2find As String 
    text2find = tofind.Text 
    Dim posicion As Integer 
    posicion = InStr(1, Text2.Text, text2find)
    If posicion > 0 Then
    With Text2
                .SelStart = posicion - 1
                .SelLength = Len(tofind)
                .SetFocus
            End With
    End If
    Then just Use Replace function to replace a string. Or if you need something else, please post more details.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192

    Re: Edit a text file

    Thanks so far, I'll explain:
    I already have found the line(string) I want to replace but I don't now how to replace it with a new string ?

  5. #5
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    813

    Re: Edit a text file

    If you're processing line-by-line, then use the Replace function to change the line.

    Code:
    Dim hFile as Integer
    Dim sFilePath as String 
    Dim sData as String 
    . . . 
    hFile = FreeFile() 
    Open sFilePath For Input As #hFile 
       Do While Not EOF( hFile )
          Line Input #hFile, sData
          
          If ( Instr( sData, "abc" ) > 0 ) Then 
             sData = Replace( sData, "abc", "xyz" )
          End If 
          
       Loop 
    Close #hFile
    Regards, Phill W.
    Last edited by Phill.W; Jan 29th, 2013 at 08:00 AM.

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Edit a text file

    Try this too:

    Code:
    Call ReplaceText("C:\TextFile.txt", "FindThisText", "ReplaceWithThis")
    
    Private Sub ReplaceText(ByRef sFileName As String, ByRef sFind As String, ByRef sReplace As String)
        Dim strData As String
    
        Open sFileName For Binary As #1
            strData = Space$(LOF(1))
            Get #1, , strData
        Close #1
    
        strData = Replace(strData, sFind, sReplace)
    
        Open sFileName For Binary As #2
            Put #2, , strData
        Close #2
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192

    Re: Edit a text file

    That's exactly what I needed !
    Thanks Bonnie,

    Regards, Kars

  8. #8
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Edit a text file

    Don't forget to mark this thread Resolved!
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

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