Results 1 to 15 of 15

Thread: searching file, return string postion

  1. #1

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    searching file, return string postion

    Is it possible to search a file for a string and return that string's position in the file?

    Also, is it possible to read from X position to Z position in a file?

    Thanks for your help!!!

  2. #2
    Hyperactive Member mikef's Avatar
    Join Date
    Jun 2000
    Location
    Beach bound...
    Posts
    510
    use the Instr function.
    "If I had known it was going to be that kinda party, I would have stuck my disk in the mash potatos!"

  3. #3
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    yup, its a doddle.

    just read the whole file into a string then use the Mid function to get the parts you want.

    i.e.

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strFile As String
    3.    
    4.     Open "C:\test.txt" For Binary As #1
    5.         strFile = Input(LOF(1), #1)
    6.     Close #1
    7.    
    8.     'this will display from character 10 to 19 (10 chars)
    9.     MsgBox Mid$(strFile, 10, 10)
    10. End Sub
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    also to find a string in a file:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub FindInFile(strFilePath As String, strString As String)
    4.     Dim strFile As String
    5.     Dim lngPos As Long
    6.    
    7.     Open strFilePath For Binary As #1
    8.         strFile = Input(LOF(1), #1)
    9.     Close #1
    10.    
    11.     lngPos = InStr(strFile, strString)
    12.    
    13.     If lngPos > 0 Then
    14.         MsgBox "'" & strString & "' found in file: " & strFilePath & " at character position " & lngPos
    15.     Else
    16.         MsgBox "'" & strString & "' not found in file: " & strFilePath
    17.     End If
    18. End Sub
    19.  
    20. Private Sub Form_Load()
    21.     FindInFile "C:\test.txt", "bob"
    22. End Sub

    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  5. #5
    Hyperactive Member mikef's Avatar
    Join Date
    Jun 2000
    Location
    Beach bound...
    Posts
    510
    what's a doddle?
    "If I had known it was going to be that kinda party, I would have stuck my disk in the mash potatos!"

  6. #6
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    its british for "a piece of cake"

    actually british for "a piece of cake" is "a piece of p!ss"

    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  7. #7
    Hyperactive Member mikef's Avatar
    Join Date
    Jun 2000
    Location
    Beach bound...
    Posts
    510
    That's cool!!!

    On the Great Race last night they had to figure out Australian slang.... I was totally confused!!!!

    "If I had known it was going to be that kinda party, I would have stuck my disk in the mash potatos!"

  8. #8
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    i guess that makes you a Wombat then?
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  9. #9
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    oh and i do believe that our british version for that is Muppet
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  10. #10
    Hyperactive Member mikef's Avatar
    Join Date
    Jun 2000
    Location
    Beach bound...
    Posts
    510



    I used to watch the muppets on TV when I was kid.
    "If I had known it was going to be that kinda party, I would have stuck my disk in the mash potatos!"

  11. #11

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    I thought a string could only hold 256 chars, but when I did a test label1.caption = strHTML, it has all the html. I'm not sure why my code won't work:

    I can skip searching for text in a file if I can get this to work:

    const strForm as String = "<form"
    dim bytForms as Byte
    Dim intPosition As Integer
    intPosition = 0

    bytForms = 0
    'Gets number of forms in document
    Do
    intPosition = InStr(intPosition, strHTML, strForm)
    If intPosition <> 0 Then
    bytForms = bytForms + 1
    End If
    Loop Until intPosition = 0

    This gives me a Run-Time error '5', Invalid procedure call or argument.
    Any ideas?

  12. #12
    Hyperactive Member mikef's Avatar
    Join Date
    Jun 2000
    Location
    Beach bound...
    Posts
    510
    Try this:

    Code:
    intPosition = 0
    
    intForms = 0
    'Gets number of forms in document
    Do
      intPosition = InStr(intPosition + 1, strHTML, strForm)
    
      If intPosition <> 0 Then
        intForms = intForms + 1
      End If
    
      If intPosition = 0 Then Exit Do
    Loop
    
    MsgBox intForms
    Last edited by mikef; Apr 25th, 2002 at 12:44 PM.
    "If I had known it was going to be that kinda party, I would have stuck my disk in the mash potatos!"

  13. #13

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    I'll just answer my question =)

    Strings can hold ~2 billion characters.

    String positions start at 1, not 0 (like C++).

    Thanks goes to all who helped...everyone does in someway.

  14. #14
    Hyperactive Member mikef's Avatar
    Join Date
    Jun 2000
    Location
    Beach bound...
    Posts
    510
    That is what this does:

    intPosition = InStr(intPosition + 1, strHTML, strForm)

    intPosition = 0 but when you start the loop you add 1 to intPosition making it equal to 1....0 is considered a NULL value.... if you don't initialize it at 0 when you add 1 you would start at 2.

    I tested you original code and found the errors.

    Did you try the routine I posted. I did and it found all instances of <form.

    ...later
    "If I had known it was going to be that kinda party, I would have stuck my disk in the mash potatos!"

  15. #15
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Originally posted by wordracr
    I thought a string could only hold 256 chars
    actually a variable length string variable can hold anything upto 2 billion characters according to MSDN
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

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