Solved

Fixing a text file

Posted on 2000-02-14
6
166 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
Comment Utility
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
Comment Utility
Adjusted points to 120
0
 

Author Comment

by:GoofyJoe99
Comment Utility
wow, yes, post the vb5 version and you get the points. I didnt think of doing it line by line. Thanks
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Accepted Solution

by:
jjsc earned 120 total points
Comment Utility
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
Comment Utility
cool thanks!!!
0
 
LVL 1

Expert Comment

by:jjsc
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now