|
-
Apr 25th, 2002, 10:41 AM
#1
Thread Starter
Hyperactive Member
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!!!
-
Apr 25th, 2002, 10:44 AM
#2
Hyperactive Member
"If I had known it was going to be that kinda party, I would have stuck my disk in the mash potatos!"
-
Apr 25th, 2002, 10:46 AM
#3
Bouncy Member
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:
Private Sub Form_Load()
Dim strFile As String
Open "C:\test.txt" For Binary As #1
strFile = Input(LOF(1), #1)
Close #1
'this will display from character 10 to 19 (10 chars)
MsgBox Mid$(strFile, 10, 10)
End Sub
-
Apr 25th, 2002, 10:50 AM
#4
Bouncy Member
also to find a string in a file:
VB Code:
Option Explicit
Private Sub FindInFile(strFilePath As String, strString As String)
Dim strFile As String
Dim lngPos As Long
Open strFilePath For Binary As #1
strFile = Input(LOF(1), #1)
Close #1
lngPos = InStr(strFile, strString)
If lngPos > 0 Then
MsgBox "'" & strString & "' found in file: " & strFilePath & " at character position " & lngPos
Else
MsgBox "'" & strString & "' not found in file: " & strFilePath
End If
End Sub
Private Sub Form_Load()
FindInFile "C:\test.txt", "bob"
End Sub
-
Apr 25th, 2002, 10:52 AM
#5
Hyperactive Member
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!"
-
Apr 25th, 2002, 10:56 AM
#6
Bouncy Member
its british for "a piece of cake"
actually british for "a piece of cake" is "a piece of p!ss"
-
Apr 25th, 2002, 11:02 AM
#7
Hyperactive Member
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!"
-
Apr 25th, 2002, 11:10 AM
#8
Bouncy Member
i guess that makes you a Wombat then?
-
Apr 25th, 2002, 11:11 AM
#9
Bouncy Member
oh and i do believe that our british version for that is Muppet
-
Apr 25th, 2002, 11:17 AM
#10
Hyperactive Member
-
Apr 25th, 2002, 11:44 AM
#11
Thread Starter
Hyperactive Member
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?
-
Apr 25th, 2002, 12:27 PM
#12
Hyperactive Member
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!"
-
Apr 25th, 2002, 12:48 PM
#13
Thread Starter
Hyperactive Member
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.
-
Apr 25th, 2002, 02:29 PM
#14
Hyperactive Member
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!"
-
Apr 26th, 2002, 03:24 AM
#15
Bouncy Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|