SoFunction
Updated on 2025-04-13

The magical features of FSO in ASP - Search with FSO

Author: Gan Jiping

You might think: OK, now I know how to write to the file. But can we do more? Let’s try to create a search function for the web site.

The key to building a search engine is recursion. Mainly, write a piece of code to search for files in the directory, and then execute the same code on all directories loops. because
In order to be unable to determine how many subdirectories are in total, the search code must be executed over and over until it is finished. Recursive calls are very good!

Let’s create a search page. Suppose an HTML form has been created, where the user enters a search string.

Dim objFolder
Dim strSearchText
Dim objFSO

strSearchText = ("SearchText") < -- The search string
' create the FSO and Folder objects
Set fso = ("")
Set objFolder = (("/"))

Search objFolder


The above code simply initializes the variables, and the Search function executes the search function, which is described as follows:



Function Search(objFolder)

Dim objSubFolder



'loop through every file in the current
folder

For Each objFile in

Set objTextStream = (,1) < -- For Reading



'read the file's contents into a
variable

strFileContents =



'if the search string is in the file, then
write a link

' to the file

If InStr(1, strFileContents, strSearchText, 1) then

"< A HREF=""/" & & _

""">" & & "< /A>< BR>"



bolFileFound = True

End If







Next



'Here's the recursion part - for each

' subfolder in this directory, run the Search function again

For Each objSubFolder in

Search objSubFolder

Next

End Function

In order to be able to open a file, FSO requires the actual file path, not the web path. For example, it is c:inetpubwwwroot, not
/temp/ or /temp/. To convert the latter to the former, use
("filename"), filename represents the web path name.

The above code will be executed in each subdirectory of the folder under the initial directory you specify, where the initial directory refers to the web root directory "/". Then
Simply open each file in the directory to see if it contains the specified string. If the string is found, the link to that file will be displayed.

Note that as the number of files and subdirectories increases, the time it takes for searches will increase. If you need heavy search work, you are advised to take other measures
Methods, such as Microsoft's index server Index Server.