Results 1 to 11 of 11

Thread: Finding txt-files and replacing text in them

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    9

    Unhappy

    Hi,

    I'm looking for a way to search in txt files - I'm looking for a certain string - and then replace that string with another one.
    I'm trying to figure out how this goes with Get and Put, but things haven't gone too well.

    Someone out there could help me a hand?
    Thanks,
    Alain

  2. #2
    Junior Member
    Join Date
    Aug 2000
    Posts
    27
    Code:
    Text1 = Replace(Text1.Text, "string", "string2") ' This will replace string with string2

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    9

    Angry Need Change in FILE - not just in string

    Well, off course I thought about this solution myself
    May I didn't make myself clear enough: I don't need a certain string from a file and then take it and change it for some use. I need the string IN THE FILE to be changed!
    Replace will just change the string I took with Line Input, but it won;t change the tet in the file itself - and that is
    what needs to be done. I guess I need to work with get and put, but the question is how. My problem is not finding and changing the string, but having it replaced/written in the file itself.

    alain

  4. #4
    Junior Member
    Join Date
    Aug 2000
    Posts
    27
    Save the file after replacing the string

    Code:
    Open "FileName" For Append As #1
    Print #1, Text1.text
    Close #1

  5. #5
    Guest
    Fon't use Append, use Output.

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'search for a particular line and do something with it
    'read the file into an array
    'find the line you are looking for
    ' then read the array back into the file using Output
    'so you can overwrite the original file
    
    Option Explicit
    Option Compare Text
     
    
    Private Sub Form_Load()
        Dim myArr() As Variant, myLine As String
        Dim myComp As String
        Dim i As Integer, intNum As Integer
        
        myComp = "The line I am Looking for"
        intNum = FreeFile
    '
    'open file
        Open "C:\myfile.txt" For Input As intNum
        
       Do While Not EOF(intNum)
            i = i + 1
            ReDim Preserve myArr(1 To i) As Variant
            Line Input #intNum, myLine
            myArr(i) = myLine
           
            myArr(i) = Trim(myArr(i))
            If myArr(i) = myComp Then
    'do whatever you want to here I change it to read "My Fixed Line"
            myArr(i) = "My Fixed Line"
            
            End If
            
        Loop
            Close #intNum
    
     'open file and save new contents
     
        Open "c:\myfile.txt" For Output As intNum
            For i = 1 To UBound(myArr)
                Print #intNum, myArr(i)
    
            Next
        Close #intNum
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Could be done easier too :
    Code:
    Dim buffer as string
    Open "c:\myfile.txt" for binary as #1
       buffer=space(lof(1))
       Get#1,,buffer
       Replace(buffer, "what you want to find", "what you want to replace it with")
       Put#1,1,buffer
    close #1
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Guest
    You forgot to replace the buffer with the updated version.
    Code:
    Buffer = Replace(buffer, "what you want to find", "what you want to replace it with")

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Red face

    Sorry, things happens, maybe i'm just tired
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Guest
    There is a problem with your method. Eg:
    The text-file contains: "One big step for man"
    Your two buffers might be for example:
    1) "One big st"
    2) "ep for man"
    What happens if the user wants to find the string "step" ?........

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I guess you don't mean my method? Hesaidjoe's search for whole lines...
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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