Results 1 to 12 of 12

Thread: A few simple string/file manipulation questions

  1. #1

    Thread Starter
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209

    A few simple string/file manipulation questions

    Here's a list of a few questions I've got.

    1. How do I replace the complete contents of a file with something else?
    2. How do I parse HTML (eg.: Parse the <TITLE></TITLE> tags and display the contents in a text box, and loop it over, and if there's more than 1 <TITLE></TITLE> tag, load those into more textboxes.
    Last edited by eiSecure; Dec 22nd, 2001 at 10:02 PM.

  2. #2
    DaoK
    Guest
    1. How do I replace the complete contents of a file with something else?
    Shame on you Ei shame on you

    VB Code:
    1. Dim MyTempoStr As String
    2.  
    3. Open "c:\test.txt" For Input As #1
    4.     MyTempoStr = Input(LOF(1), 1)
    5. Close #1
    6.  
    7. MyTempoStr = Replace(MyTempoStr, vbCrLf, " ")
    8.  
    9. Open "c:\test2.txt" For Output As #1
    10.     Print #1, MyTempoStr
    11. Close #1

  3. #3

    Thread Starter
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209


    Now for number 2.
    That's a tricky.

  4. #4
    DaoK
    Guest
    Search with Instr the <TITLE> and </TITLE> and with MID take all stuff between...

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by DaoK


    Shame on you Ei shame on you

    VB Code:
    1. Dim MyTempoStr As String
    2.  
    3. Open "c:\test.txt" For Input As #1
    4.     MyTempoStr = Input(LOF(1), 1)
    5. Close #1
    6.  
    7. MyTempoStr = Replace(MyTempoStr, vbCrLf, " ")
    8.  
    9. Open "c:\test2.txt" For Output As #1
    10.     Print #1, MyTempoStr
    11. Close #1
    You don't need to open you file, just kill the file and write a new one with what you want.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    DaoK
    Guest
    I know that because I have Close#1 but it is cool to compare after both file... It's just an example...

  7. #7

    Thread Starter
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    Actually, DaoK, I'm looking for efficiency above explaining.

  8. #8

    Thread Starter
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    Originally posted by DaoK
    Search with Instr the <TITLE> and </TITLE> and with MID take all stuff between...
    It would be sooo much easier if there was a function for this...

  9. #9
    DaoK
    Guest
    VB Code:
    1. Dim theString As String
    2. Dim startCount As Integer
    3. Dim endCount As Integer
    4. Dim block1 As String
    5. Dim block2 As String
    6. Dim i As Integer
    7.  
    8. block1 = "<Title>"
    9. block2 = "</Title>"
    10. theString = block1 & vbCrLf & "asdf" & vbCrLf & block2 ' That should be your html file here
    11. startCount = 1
    12. For startCount = 1 To Len(theString)
    13.     startCount = InStr(startCount, theString, "[Start]", vbTextCompare)
    14.     If startCount = 0 Then Exit For
    15.     endCount = InStr(startCount, theString, "[End]", vbTextCompare)
    16.     Debug.Print Mid(theString, startCount + Len(block1), endCount - (startCount + Len(block1)))
    17.     startCount = endCount
    18. Next startCount
    That will search and put in debug.print all the text between the tag

  10. #10

    Thread Starter
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    Yes, but it's not good for looping.

    When you loop the code, there always seems to be problems, and part of the text doesn't show up.

  11. #11
    DaoK
    Guest
    VB Code:
    1. Private Function BetweenStr(block1 As String, block2 As String) As String
    2. Dim theString As String
    3. Dim startCount As Integer
    4. Dim endCount As Integer
    5. Dim i As Integer
    6. block1 = "<Title>"
    7. block2 = "</Title>"
    8. theString = block1 & vbCrLf & "asdf" & vbCrLf & block2 ' That should be your html file here
    9. startCount = 1
    10. startCount = InStr(startCount, theString, "[Start]", vbTextCompare)
    11. If startCount = 0 Then Exit For
    12. endCount = InStr(startCount, theString, "[End]", vbTextCompare)
    13. BetweenStr = Mid(theString, startCount + Len(block1), endCount - (startCount + Len(block1)))
    14. End Function

  12. #12

    Thread Starter
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    DaoK, even WITH removing your re-declarations of block1 and block2, your code doesn't loop well.

    Almost every time, I get words that are cut off by a few characters.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width