Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Fixing a text file

Posted on 2000-02-14
6
Medium Priority
?
199 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Accepted Solution

by:
jjsc earned 480 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

916 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