Solved

Fixing a text file

Posted on 2000-02-14
6
175 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

829 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