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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find using Reg Exp 1

Status
Not open for further replies.

richardii

Programmer
Jan 8, 2001
104
GB
I'm using this code to find a string in a file. It works fine as long as the file is NOT a large binary file!! Unfortunately that's exactly what I want to look in. Any ideas what the problem might be/solution.

Thanks in anticipation.


Option Explicit

Dim objRegExpr
Dim FSO, TF, inp
Dim colMatches

Set objRegExpr = New regexp

objRegExpr.Pattern = "ADWmax"
objRegExpr.Global = True

Set FSO=CreateObject("Scripting.FileSystemObject")
Set TF=FSO.OpenTextFile("ADwrite",1) ' 1=ForReading
inp=TF.ReadAll


'Execute the regular expression search
Set colMatches = objRegExpr.Execute(inp)

'Print the # of matches found
Msgbox colMatches.Count & " matches found..."


Set colMatches = Nothing
Set objRegExpr = Nothing
 
what does response.write(inp) give you? the expected text of the file?

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Thanks - I've given up on this approach. It will "work" as long as the file is text - but it's too slow.
 
Care to give some stats for "too slow" ?

I'd be interested in whether Reg Exps were faster than InStr() type searching for large plain-text files.

Anyone already tested this?

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
havent needed to as yet. i would say if you were only interested in if the file contains the string then reading a line at a time and then dropping out when you find the string might be quicker but i guess thats not what you want.
 
It was taking about 3-4 minutes to find 3 occurences in a 1,230 KB file, which did not compare with a UNIX grep at all well! I've not tried the Instr() approach.
 
How long did it take to just load the file into a string? (ie remove the reg exp bit)

I'd say quite a bit.. not sure what the best way of searching text files with VBscript is.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
That's interesting. Nearly *all* of the time is spent reading the string in.
 
Maybe a line-by-line approach will speed things up?

Read in a line -> do search -> repeat.

Anyone else know an efficient way to search files with vbScript?

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
The idea of searching by line is a good one - and in fact all I'm interested in is whether the string occurs (not how many times). I've done that and it works fine - the problem comes if the file is binary (I then get the message "Input past end of file"). Big text files are fine.

Code:
Option Explicit

Dim objRegExpr
Dim FSO, TF, inp, MyFile
Dim colMatches

Set objRegExpr = New regexp
objRegExpr.Pattern = "ADWmax"
objRegExpr.Global = True


Set FSO=CreateObject("Scripting.FileSystemObject")
Set MyFile=FSO.GetFile("ADwrite")

Set TF=FSO.OpenTextFile("ADwrite")

colMatches = 0

Do
  inp=TF.Readline
  'Execute the regular expression search
  Set colMatches = objRegExpr.Execute(inp)
  If ColMatches.count = 1 then
     'Quit loop as soon as found
     Exit Do
  End if	
loop


Msgbox colMatches.count

Set colMatches = Nothing
Set objRegExpr = Nothing

 
I've never done any file related work with VBscript, but at a guess you are trying to read text files even if you point it at a binary file?

See this page:

and maybe this one for more links:

let us know how you get on


Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Thanks very much - that first excellent link did the trick. I used ReadBinaryFile(cFileToSearch) to read it and RSBinaryToString(xBinary) to convert to a string. It's fast (5 seconds).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top