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

Read Bytes Method? 2

Status
Not open for further replies.

acewilli

IS-IT--Management
Apr 11, 2003
98
US
Hello, I know this is the fastest way to search a large file but how do I display the lines that I am searching for? Thanks!

Code:
const ForReading = 1
const TristateFalse = 0
dim strSearchThis
dim objFS
dim objFile
dim objTS
set objFS = Server.CreateObject("Scripting.FileSystemObject")
set objFile = objFS.GetFile(Server.MapPath("myfile.txt"))
set objTS = objFile.OpenAsTextStream(ForReading, TristateFalse)

strSearchThis = objTS.Read(objFile.Size)

if instr(strSearchThis, "keyword") > 0 then
    Response.Write "Found it!"
end if
 
...
Set objTS = objFile.OpenAsTextStream(ForReading, TristateFalse)
While Not objTS.AtEndOfStream
strSearchThis = objTS.ReadLine
If InStr(strSearchThis, "keyword") > 0 Then
Response.Write strSearchThis
End If
WEnd
objTS.Close

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
instead of reading byte method, read one line at a time. then use the same method of instr to scan each_line for the keyword. if > 0 then msgbox the whole line.
is this what you want?
 
Or a direct response to the setting the question presumes.
[tt]
'etc etc
strSearchThis = objTS.Read(objFile.Size)

set rx=new regexp
with rx
.global=true
.multiline=true
.pattern="^.*?" & keyword & ".*?$" 'supposing keyword is well-behave, else do some escaping
.ignorecase=true 'or false, depends on need
end with

set cm=rx.execute(s)
if cm.count<>0 then
for i=0 to cm.count-1
response.write "[" & i & "] " & cm(i) & "<br />"
next
else
response.write "no match" & "<br />"
end if
[/tt]
 
Amendment

The corresponding line should be read like this.
[tt] set cm=rx.execute([blue]strSearchThis[/blue])[/tt]

 
Thanks everyone. Here is how I decided to do it and it works great. If you see something wrong with this please let me know. It seems to work great. Also...if anyone can tell me how to search for more than one keyword I would very much appreciate it. Thanks again!

Code:
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<center><P><H3>Search Results</H3></P></center>
<center>You have searched for: <B><%=Request("TextToSearch")%> on <%=Request("Select")%></B></center>
<center><a href="search.asp">Back</a></center>
<hr>
<%
Dim strtextToSearch
Dim strselect
Dim strfile
Dim strcmd

'strtextToSearch = Request("TextToSearch")
strselect = Request("Select")
'Now, we want to search all of the files
Dim fso

Const ForReading = 1
Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Dim oWSH
    Set oWSH= Server.CreateObject("WScript.Shell")

'Specify the folder path to search.
Dim FolderToSearch
set objNet=Server.CreateObject("WScript.Network")

If strselect = "OICU8120B00BD5A3B8B" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\sitea\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\sitea\output.txt"
    oWSH.Run strcmd, 0, True
    strfile = "\bp\sitea\output.txt"
elseif strselect = "OICU8120D0065A9FEE1" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\siteb\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\siteb\output.txt"    
    oWSH.Run strcmd, 0, True
    strfile = "\bp\siteb\output.txt"
elseif strselect = "OICU8120D0065EFC8FE" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\sitec\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\sitec\output.txt"    
    oWSH.Run strcmd, 0, True
    strfile = "\bp\sitec\output.txt"
elseif strselect = "OICU8120F00F1732570" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\sited\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\sited\output.txt"    
    oWSH.Run strcmd, 0, True
    strfile = "\bp\sited\output.txt"
elseif strselect = "OICU8120600B57DABCA" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\sitee\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\sitee\output.txt"    
    oWSH.Run strcmd, 0, True
    strfile = "\bp\sitee\output.txt"
elseif strselect = "OICU8120D0065B9FBF6" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\sitef\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\sitef\output.txt"    
    oWSH.Run strcmd, 0, True
    strfile = "\bp\sitef\output.txt"
elseif strselect = "OICU812PCASOB30LY41" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\siteg\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\siteg\output.txt"    
    oWSH.Run strcmd, 0, True
    strfile = "\bp\siteg\output.txt"	
elseif strselect = "OICU8120D00651D79F3" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\siteh\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\siteh\output.txt"    
    oWSH.Run strcmd, 0, True
    strfile = "\bp\siteh\output.txt"	
elseif strselect = "OICU8120D00655D09F2" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\sitei\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\sitei\output.txt"    
    oWSH.Run strcmd, 0, True
    strfile = "\bp\sitei\output.txt"	
	elseif strselect = "OICU812PTXWL9MTQY41" then
strtextToSearch = Request("TextToSearch")
    FolderToSearch = "C:\bp\sitej\BP2000.log"
    strcmd = "%comspec% /Q /C find /I " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch & " >C:\bp\sitej\output.txt"    
    oWSH.Run strcmd, 0, True
    strfile = "\bp\sitej\output.txt"			
end if

const TristateFalse = 0
dim strSearchThis
dim strline
dim objFile
dim objTS
dim thing

set objFile = FSO.GetFile(Server.MapPath(strfile))
set objTS = objFile.OpenAsTextStream(ForReading, TristateFalse)

thing = objTS.Read(objFile.size)
strSearchThis = Replace(thing,vbcrlf,"<BR><HR>") '(objFile.size)
Response.write "<center>Please wait...this could take a minute.</center>"
Response.write strSearchThis

set objfile = nothing
set objts = nothing
Set fso = Nothing
set strfile = nothing
set oWSH = nothing
%>
<center><a href="search.asp">Back</a></center>
</BODY>
</HTML>
 
I don't think you've made a good decision, even the worse amongst proposed. It is sure up to you.
 
Okay...so my way is slower than your suggestion? From what I read on the 4guysfromrolla it seemed as though this was a faster way.

Let me ask this about the suggestions. Are you talking about the way I use the Shell cmd to search and create a new file or are you talking about the parsing of the new file that I create on where I made the wrong decision? Do you think I should even use the shell method or just use vbscript to find what I need? It seemed as though using the find command was faster and since the bp2000.log is constantly being written to I thought it might cause more issues.

I appreciate your help and thank you.

acewilli
 
I don't think of slower or faster. I consider dependency. You are working on a server environment and that's how I rate priority notwithstanding the speed is important. Just an opinion, and I have nothing to insist.
 
I'll give it a try and see which one will work best for what I need it to do. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top