Solved

Fixing a text file

Posted on 2000-02-14
6
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to parse out a json string in VB6? 10 390
Using "ScreenUpdating" 6 86
Exit a vb6 apps when a calling it apps closes 15 62
Zip Folders Using Chilkat Routines 1 86
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

734 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