Displaying Source Code(s)
|
|
Search Function
--------------------------------------------------------------------------------
Description : The Search function recursively searches all files
on a hard drive for a specific phrase from a specified start
directory. The Search function is not meant for web site
searches by site users but was designed to be used in code only
to obtain the path of a file matching the given phrase. The
Search function returns a string of absolute paths of all files
containing the search phrase. Each path is separated by a
carriage-return line-feed (vbCrLf) that is used as a delimiter.
The Search function has two required arguments, phrase and
directory. Phrase is a string representing these exact phrase or
word grouping to search. Directory is the absolute path of the
folder to begin recursively searching.
example usage:
Search all files of the entire web site For the exact phrase "request.cookies"
And delete them (also uses the kill statement).
<%
' declare variables
Dim a, b, i
' search the entire web site for the phrase "request.cookies"
a = Search( "request.cookies", Server.MapPath("/") )
' iterate the array of results
b = Split(a , vbCrLf)
For i = 0 To UBound(b) - 1
' delete each matching page
Kill b(i)
Next
%>
source code:
<%
Private Function Search(ByVal phrase, ByVal directory)
Dim objFSO, currentFolder, objFile, currentFile
Dim strSearch, fileContents, objFolder
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set currentFolder = objFSO.GetFolder(directory)
For Each objFile In currentFolder.Files
If LCase( objFile.Path ) = _
LCase( Server.MapPath( _
Request.ServerVariables("SCRIPT_NAME") ) ) Then
Else
Set currentFile = _
objFSO.OpenTextFile( objFile.Path, 1, False )
fileContents = LCase( currentFile.ReadAll() )
currentFile.Close
Set currentFile = Nothing
If InStr( fileContents, phrase ) Then
strSearch = strSearch & objFile.Path & vbCrLf
Else
strSearch = strSearch & ""
End If
End If
Next
For Each objFolder In currentFolder.SubFolders
strSearch = strSearch & Search( phrase, objFolder )
Next
Set currentFolder = Nothing
Set objFSO = Nothing
Search = CStr( strSearch )
End Function
%>
-------------------------------------------------------------------------------- |
|
|