I am trying to search for a string (regular expression) in a txt file with around 25,000 lines. I need it to return the entire line if it finds a match. I am using the .AtEndofStream and .ReadLine but it takes forever, maybe 10 mintues. What would be a better approach?
You are processing 41 lines per second on an unorganized file, I don't think you are going to get any faster than that without changing your file's format to a more organized structure (xml, csv, etc..)
A good rule of thumb is to never measure with your thumb.
I have the same question actually. However my files are even bigger. One that I'm looking at is 672,000 lines and growing. I don't need the contents of the line which the file is found, just whether or not the string is found.
One thought I had was to get just the last 1000 lines or so, but it seems with vbscript you still have to read the whole file?
It literally takes 2 seconds in unix to perform a search operation on the same file, there must be something in Windows which is comparable?
This is kind of a hack, but it works. It doesn't return the line, but you could easily read the file that the search is writing out to. For my purposes I only need to know whether or not a string was found.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(filename) Then
print "my file exists"
print "no file found"
logpath="C:\Documents and Settings\dmfranko\Desktop\search.log"
command="findstr " & text & " " & filename & " > " & """" & logpath & """"
call f_append_to_file("C:\Documents and Settings\dmfranko\Desktop\search.bat",command)
SystemUtil.Run ("C:\Documents and Settings\dmfranko\Desktop\search.bat")
Set objFile = objFSO.GetFile("C:\Documents and Settings\dmfranko\Desktop\search.log")
If objFile.Size > 0 Then
objFSO.DeleteFile("C:\Documents and Settings\dmfranko\Desktop\search.bat")
objFSO.DeleteFile("C:\Documents and Settings\dmfranko\Desktop\search.log")