Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find a string in an HTML file

Status
Not open for further replies.

imarg

Technical User
Jun 5, 2006
18
CA
Hi everyone... I am completely stuck on this and cannot find a solution that works anywhere.

I am looking for a way to open a locally saved html file, and get it to look for a particular string and then if it finds that string then do something else with it....

Does anyone know any code that will do this.

Thanks,

Imarg
 
The is modified from what Golom posted in vb forum for you. It is also modified so that search is done case-insensitive.
[tt]
Dim FoundIt 'as boolean
With createobject("Scripting.FileSystemObject")
FoundIt = (InStr(1,.OpenTextFile("C:\...\myFile.html",1,true,-2).ReadAll,"SomeString",1) <> 0)
End With
[/tt]
 
Hi tsuji,

It seems to work for single characters, but for some reason it refuses to find any actual strings in my text documents... which is strange.
 
>It seems to work for single characters, but for some reason it refuses to find any actual strings in my text documents... which is strange.
I would be surprised if it works only for single character. It should work as well for a string.

Two problems though might arise.

[1] If the file being inexisting and/or of size zero (empty file, in case of inexisting, the newly created one would also be empty). Then .readall may fail. Hence, to make it more robust, control of those two factors may help. if you want to retain the one-line appeal, you can put on error control.
[tt]
Dim FoundIt 'as boolean
FoundIt=false 'initialize it to false
With createobject("Scripting.FileSystemObject")
on error resume next
FoundIt = (InStr(1,.OpenTextFile("C:\...\myFile.html",1,true,-2).ReadAll,"SomeString",1) <> 0)
on error goto 0
End With
[/tt]
[2] The format unicode/ascii the particular file was being saved. The above use system default (parameter -2). But on winnt series, it might be defaulted to unicode whereas somehow if the html file was saved in ascii, it would fail. And the same vice versa.

So if you are sure the file exists and that the result is unexpected, you should look into the parameter -2. Try change it to 0 and -1 successively (depending on the os.) That would be the source of the problem.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top