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!!!
Printable View
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!!!
use the Instr function.
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
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
:)
what's a doddle? :confused:
its british for "a piece of cake"
actually british for "a piece of cake" is "a piece of p!ss"
:D
That's cool!!!
On the Great Race last night they had to figure out Australian slang.... I was totally confused!!!!
:)
i guess that makes you a Wombat then?
oh and i do believe that our british version for that is Muppet
:confused: :confused: :confused:
I used to watch the muppets on TV when I was kid. ;)
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?
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
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.
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
actually a variable length string variable can hold anything upto 2 billion characters according to MSDN :eek:Quote:
Originally posted by wordracr
I thought a string could only hold 256 chars