1
2Response.Buffer=True
3
4'
5' OneFile Search Engine (ofSearch v1.0)
6' Copyright ?000 Sixto Luis Santos
<[email protected]>
' All Rights Reserved
'
' Note:
' This program is freeware. This program is NOT in the Public Domain.
' You can freely use this program in your own site.
'
' You cannot re-distribute the code, by any means,
' without the express written authorization by the author.
'
' Use this program at your own risk.
'
' Globals --------------------------------------
' ----------------------------------------------
Const ValidFiles = "htmltxt"
Const RootFld = "./"
Dim Matched
Dim Regex
Dim GetTitle
Dim fs
Dim rfLen
dim RootFolder
Dim DocCount
Dim DocMatchCount
Dim MatchedCount
' ----------------------------------------------
' Procedure: SearchFiles()
' ----------------------------------------------
Public Sub SearchFiles(FolderPath)
Dim fsFolder
Dim fsFolder2
Dim fsFile
Dim fsText
Dim FileText
Dim FileTitle
Dim FileTitleMatch
Dim MatchCount
Dim OutputLine
' Get the starting folder
Set fsFolder = fs.GetFolder(FolderPath)
' Iterate thru every file in the folder
For Each fsFile In fsFolder.Files
' Compare the current file extension with the list of valid target files
If InStr(1, ValidFiles, Right(fsFile.Name, 3), vbTextCompare) > 0 Then
DocCount = DocCount + 1
' Open the file to read its content
Set fsText = fsFile.OpenAsTextStream
FileText = fsText.ReadAll
' Apply the regex search and get the count of matches found
MatchCount = Regex.Execute(FileText).Count
MatchedCount = MatchedCount + MatchCount
If MatchCount > 0 Then
DocMatchCount = DocMatchCount + 1
' Apply another regex to get the html document's title
Set FileTitleMatch = GetTitle.Execute(FileText)
If FileTitleMatch.Count > 0 Then
' Strip the title tags
FileTitle = Trim(replace(Mid(FileTitleMatch.Item(0),8),"","",1,1,1))
' In case the title is empty
If FileTitle = "" Then
FileTitle = "No Title (" & fsFile.Name & ")"
End If
Else
' Create an alternate entry name (if no title found)
FileTitle = "No Title (" & fsFile.Name & ")"
End If
' Create the entry line with proper formatting
' Add the entry number
OutputLine = " <b>" & DocMatchCount & ".</b> "
' Add the document name and link
OutputLine = OutputLine & "<a "="" ")="" &="" ","="" chr(34)="" href=" & chr(34) & RootFld & replace(Mid(fsFile.Path,
rfLen),"><b>"
OutputLine = OutputLine & FileTitle & "</b></a>"
' Add the document information
OutputLine = OutputLine & "<font size="1"><br/> Criteria matched " & MatchCount
& " times - Size: "
OutputLine = OutputLine & FormatNumber(fsFile.Size / 1024,2 ,-1,0,-1) & "K bytes"
OutputLine = OutputLine & " - Last Modified: " & formatdatetime
(fsFile.DateLastModified,vbShortDate) & "</font><br/>"
' Display entry
Response.Write OutputLine
Response.Flush
End If
fsText.Close
End If
Next
' Iterate thru each subfolder and recursively call this procedure
For Each fsFolder2 In fsFolder.SubFolders
SearchFiles fsFolder2.Path
Next
Set FileTitleMatch = Nothing
Set fsText = Nothing
Set fsFile = Nothing
Set fsFolder2 = Nothing
Set fsFolder = Nothing
End Sub
' ----------------------------------------------
' Procedure: Search()
' ----------------------------------------------
Sub Search(SearchString)
Dim i
Dim fKeys
Dim fItems
Set fs = CreateObject("Scripting.FileSystemObject")
Set GetTitle = New RegExp
Set Regex = New RegExp
With Regex
.Global = True
.IgnoreCase = True
.Pattern = Trim(SearchString)
End With
With GetTitle
.Global = False
.IgnoreCase = True
.Pattern = "<title>(.|\n)*</title>"
End With
RootFolder = Server.MapPath(RootFld)
If Right(RootFld,1) <> "/" Then
RootFld = RootFld & "/"
End If
If Right(RootFolder, 1) <> "" Then
RootFolder = RootFolder & ""
End If
rfLen = Len(RootFolder) + 1
SearchFiles RootFolder
If MatchedCount = 0 Then
Response.Write " <b>No Matches Found.</b><br/>"
End If
Set Regex = Nothing
Set GetTitle = Nothing
Set fs = Nothing
End Sub
1<html>
2<head>
3<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
4<meta content="en-us" http-equiv="Content-Language"/>
5<title>OneFile Search 1.0</title>
6</head>
7<body bgcolor="#FFFFFF" link="#660000" vlink="#008000">
8<font face="Tahoma,Arial" size="2">
9<table border="0" cellpadding="0" cellspacing="0" width="100%">
10<tr>
11<td colspan="2" width="100%"></td>
12</tr>
13<tr>
14<td bgcolor="#000000" width="50%">
15<form method="Get">
16<table border="0" width="100%">
17<tr>
18<td align="right" width="33%"><font color="#FFFFFF" face="Tahoma,Arial" size="2"><b>Search
19for </b></font></td>
20<td width="33%"><input ```"="" name="query" query")="" size="20" type="text" value="```
21=Request.QueryString("/></td>
22<td width="34%"><input name="Search" type="submit" value="Search"/></td>
23</tr>
24</table>
25</form>
26</td>
27<td bgcolor="#000000" width="50%"></td>
28</tr>
29<tr>
30<td bgcolor="#000000" colspan="2" width="100%"></td>
31</tr>
32<tr>
33<td bgcolor="#808080" width="50%">
34<table border="0" width="100%">
35<tr>
36<td align="right" width="33%"><font color="#FFFFFF" face="Tahoma,Arial" size="1"><b>Tip:</b></font></td>
37<td width="67%"><font color="#FFFFFF" face="Tahoma,Arial" size="1">Search by using <a href="http://msdn.microsoft.com/scripting/default.htm?/scripting/VBScript/doc/jsgrpregexpsyntax.htm">Regula
38r Expresions</a>.</font></td>
39</tr>
40</table>
41</td>
42<td bgcolor="#808080" width="50%"></td>
43</tr>
44</table>
45
If Trim(Request.QueryString("query")) <> "" Then
1<hr/>
2<table bgcolor="#808080" border="0" cellpadding="0" cellspacing="0" width="100%">
3<tr>
4<td width="100%"><font color="#FFFFFF" size="2"> Your search for <b>```
5
6=Request.QueryString("query")
7```</b> found the following documents:</font></td>
8</tr>
9</table>
10<br/><br/>
Response.Flush
Search Request.QueryString("query")
If DocCount > 0 Then
1<br/>
2<font size="1">
3 (The search criteria "```
4=Request.QueryString("query")
5```" found ```
6=MatchedCount
7``` times in ```
8
9=DocMatchCount
10``` of ```
11=DocCount
12``` documents.)
13</font>
End If
End If
1<br/><br/>
2<hr/><div align="center">
3<font size="1">
4OneFile Search Engine v1.0<br/>
5Copyright?000 <a href="mailto:[email protected]">Sixto Luis Santos</a>.
6All Rights Reserved
7</font></div>
8</font>
9</body>
10</html>
11
Response.End