Dim fso, fol ' initial fso and folder objects
Dim searchStr' substring we are looking for
Dim result 'this will be the path to the first match
Set fso = Server.CreateObject("Scripting.FileSystemObject")
'pretend the searchStr got set in Request.Form("txtSearch")
searchStr = Request.Form("txtSearch")
'start the search with current directory
Set fol = fso.GetFolder(Server.MapPath("."))
'search recursively - see below for the actual function that handles the searching
result = FileSearch(searchStr,fol)
'if len(result) > 0 then w have a match, lets print a link
If len(result) > 0 Then
'since we are only searching a web dirctory we can make this a download link
Response.Write "File Found: <a href=""" & result & """>" & result & "</a>"
End If
'function that handles the actual searching that is called above
Function FileSearch(str_search, current_fol)
Dim fil, fol 'temp file and folder objects
Dim temp 'temp string for subfolder searching
'search the current directory for a match first
For Each fil in current_fol.Files
'If one matches, return it and exit
If InStr(fil.Name,str_search) > 0 Then
FileSearch = fil.Name
Exit Function
End If
Next
'If it gets this far we haven't found it, so search the subfolders
For Each fol in current_fol.Folders
'If this sub-folder returns a result for FileSearch
' than it had a match so return it and exit
temp = FileSearch(str_search,fol)
If len(temp) > 0 Then
'pass back this folder name as part of the path to the file so we can make a link later
FileSearch = fol.Name & "/" & temp
Exit Function
End If
Next
'no files were matched, no files in subfolders were matched, return an empty string
FileSearch = ""
End Function