Solved

Fixing a text file

Posted on 2000-02-14
6
178 Views
Last Modified: 2010-05-02
I want to open up a text file that has strings such as <dog> or <cat> and replace these strings with what the user wants to replace them with and then save the file as that. Is there a way I can do this without first pulling the file as one huge string, messing with the string then writing the file as the new string. Id rather open the file and replace the parts with new parts as I go along. How can i set this up? help
0
Comment
Question by:GoofyJoe99
  • 3
  • 3
6 Comments
 
LVL 1

Expert Comment

by:jjsc
ID: 2519757
So far as I can see (off the top of my head) the easiest (and relatively efficient) way to do this would be to store the strings you want to store both the find and replace strings in arrays, and read/replace in your text file line by line.

For example, assuming you've opened your input and output files already [e.g.
    Open "infile.txt" For Input As #1
    Open "outfile.txt" For Output As #2
]

Next, read the input file line by line and globally replace any occurrences of your strings as follows (note, this assumes you have VB6 in order to use the Replace statement. If you don't let me know and I'll post a VB5 verion)

' assumes sFind() contains the strings to look for and
' sReplace() contains the strings to substitute with.

    Do While Not EOF(1)
        Dim i As Integer, nextLine As String
       
        Line Input #1, nextLine
        For i = LBound(sFind) To UBound(sFind)
            nextLine = Replace(nextLine, sFind(i), sReplace(i), compare:=vbTextCompare)
        Next i
        Print #2, nextLine
    Loop
    Close 2
    Close 1


Hope this gives you an idea...

John
0
 

Author Comment

by:GoofyJoe99
ID: 2521486
Adjusted points to 120
0
 

Author Comment

by:GoofyJoe99
ID: 2521487
wow, yes, post the vb5 version and you get the points. I didnt think of doing it line by line. Thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
jjsc earned 120 total points
ID: 2522297
OK, here's a function for VB5 (I hope! It's been modified a couple of times, and since I don't have VB5 installed any more I can't check it.)

Try the function (the parameters are equal to those offered by the VB6 Replace function, so it should be a drop-in replacement) and let me know if you have any problems.


'-------------------------------------
   
Public Function VB5Replace(Expression As String, FindStr As String, ReplaceStr As String, Optional Start As Long = 1, Optional Count As Long = -1, Optional CompareMethod As vbCompareMethod = vbBinaryCompare) As String
    'ReplaceStr(expressionAsString, findAsString, ReplaceAsString, optStartAsLong1, optCountAsLongNeg1, optCompareAsVbCompareMethodvbBinary)
    Dim sExpr As String, nStart As Long, nNext As Long, nCount As Long
    Dim nFindLen As Long, nReplaceLen As Long
   
    nStart = Start
    nFindLen = Len(FindStr)
    nReplaceLen = Len(ReplaceStr)
    sExpr = Expression
    nCount = Count
    Do
        nNext = InStr(nStart, sExpr, FindStr, CompareMethod)
        If nNext Then
            Dim sBefore As String
           
            If nNext = 1 Then
                sBefore = ""
            Else
                sBefore = Left$(sExpr, nNext - 1)
            End If
            sExpr = sBefore & ReplaceStr & Mid$(sExpr, nNext + nFindLen)
            nStart = nNext + Len(nReplaceLen) - 1
           
            If nCount > -1 Then
                nCount = nCount - 1
            End If
        End If
    Loop Until nNext = 0 Or nCount = 0
    VB5Replace = sExpr
End Function
0
 

Author Comment

by:GoofyJoe99
ID: 2525179
cool thanks!!!
0
 
LVL 1

Expert Comment

by:jjsc
ID: 2526352
Glad to be of assistence :-)

Let me know if you have any problems with the VB5Replace (and if not, you can change the name to just Replace and use it as a drop-in replacement for VB6 code)

John
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question