Results 1 to 7 of 7

Thread: text file replace

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    49

    text file replace

    hello
    i am trying to replace spacific character in a text file
    the text file contain several lins in a known format:
    aaa ~ bbb ~ ccc ~ ddd ~ eee
    fff ~ ggg ~ hhh ~ iii ~ jjj
    and so on
    i need to replace in the text file only the second "~" in each line to be "-":
    aaa ~ bbb - ccc ~ ddd ~ eee
    fff ~ ggg - hhh ~ iii ~ jjj

    using the "replace" function won't help here
    any idea?

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: text file replace

    Use Split() to split the string into an array using ~ as the delimiter. Then join it manually into a string using a for loop, but on the second iteration instead of joining with ~ join them with -

    Just of the top of my head. There's probably a hundred more ways of going about it.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: text file replace

    If your files aren't very large, Mid$() can work well in this case.... with some other code. Steps look like following:

    1) Read entire file into memory
    2) Split() it on carriage returns
    3) Loop thru each line, use InStr() to locate 2nd occurrence & replace it with Mid$()
    4) When done looping, Join() array back to a string & write the string to file

    Edited: similar approach to baja_yu
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: text file replace

    Quote Originally Posted by assafbe View Post
    ...using the "replace" function won't help here...
    It really depends how you use it:
    Code:
    Private Sub Command1_Click()
    Dim fileNum As Integer
    Dim sText As String
    Dim arLines() As String
    Dim iPos As Long
    Dim i As Long
    
        fileNum = FreeFile()
        Open "c:\test.txt" For Input As #fileNum
            sText = Input(LOF(fileNum), #fileNum)
        Close #fileNum
        
        arLines() = Split(sText, vbNewLine)
        For i = 0 To UBound(arLines)
            iPos = InStr(InStr(1, arLines(i), "~") + 1, arLines(i), "~")
            arLines(i) = VBA.Left$(arLines(i), iPos - 1) & Replace(arLines(i), "~", "-", iPos, 1)
            '''Debug.Print arLines(i)
        Next i
        
        sText = Join(arLines(), vbNewLine)
        fileNum = FreeFile()
        Open "c:\test.txt" For Output As #fileNum
            Print #fileNum, sText
        Close #fileNum
    
    End Sub

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: text file replace

    I suggest this for the loop: (safer and a bit faster)
    Code:
        For i = 0 To UBound(arLines)
            iPos = InStr(arLines(i), "~")
            If iPos Then iPos = InStr(iPos + 1, arLines(i), "~")
            If iPos Then Mid$(arLines(i), iPos, 1) = "-"
        Next i
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: text file replace

    Quote Originally Posted by anhn View Post
    I suggest this for the loop: (safer and a bit faster)
    Code:
        For i = 0 To UBound(arLines)
            iPos = InStr(arLines(i), "~")
            If iPos Then iPos = InStr(iPos + 1, arLines(i), "~")
            If iPos Then Mid$(arLines(i), iPos, 1) = "-"
        Next i
    +1. Truly exceptional code. Instr() and Mid$() are not to be denied when used effectively as shown here.
    Doctor Ed

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: text file replace

    Quote Originally Posted by anhn View Post
    I suggest this for the loop: (safer and a bit faster)
    I agree with your "sentiments" however my sample code was basically an "answer" to OP's comments (using the "replace" function won't help here) and was not a quest for efficiency.

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