How to use FSO to search hard disk files? Do you know how to search hard disk files for FSO? If you don’t understand it yet, follow the editor of Foxin and I will understand it.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
dim st
st=timer()
'******************************************************************
'***************Search hard disk file class SearchFile ************
'************ Call method: ************
'************Set newssearch=new SearchFile 'Declaration************
'************newsearch.Folder="F:+E:"'Passed in search source****************
'************newsearch.keyword="Compilation" 'Keyword*********
'************newsearch.Search 'Start search************
'************Set newssearch=Nothing 'end*************
'******************************************************************
Class SearchFile
dim Folders 'Passing an absolute path, multipath uses + sign to connect, no spaces
dim keyword 'Passing keywords
dim objFso 'Define global variables
dim Counter 'Define global variables, the number of search results
'*****************initialization**************************************
Private Sub Class_Initialize
Set objFso=Server.CreateObject("Scripting.FileSystemObject")
Counter=0 'Initialize the counter
End Sub
'*********************************************************************
Private Sub Class_Terminate
Set objFso=Nothing
End Sub
'****************** Public member, method called *******************************
Function Search
Folders=split(Folders,"+") 'Convert to array
keyword=trim(keyword) 'Remove the spaces before and after
if keyword="" then
Response.Write("<font color='red'> keyword cannot be empty</font><br/>")
exit Function
end if
'Just determine whether illegal characters are included
flag=instr(keyword,"") or instr(keyword,"/")
flag=flag or instr(keyword,":")
flag=flag or instr(keyword,"|")
flag=flag or instr(keyword,"&")
if flag then 'The keyword cannot contain /:|&
Response.Write("<font color='red'> keyword cannot contain /:|&</font><br/>")
Exit Function 'If this is included, exit
end if
'Multi-path search
dim i
for i=0 to ubund(Folders)
Call GetAllFile(Folders(i)) 'Call loop recursive function
next
Response.Write("A total of <font color='red'>"&Counter&"</font> results were found)
End Function
'******************************************
Private Function GetAllFile(Folder)
dim objFd,objFs,objFf
Set objFd=objFso.GetFolder(Folder)
Set objFs=objFd.SubFolders
Set objFf=objFd.Files
'Travel through subfolders
dim strFdName 'Declare the subfolder name
'************Visit subfolders*******
on error resume next
For Each OneDir In objFs
strFdName=OneDir.Name
'The system folder is not included in the traversal
If strFdName<>"Config.Msi" EQV strFdName<>"RECYCLED" EQV strFdName<>"RECYCLER" EQV strFdName<>"System Volume Information" Then
SFN=Folder&""&strFdName 'Absolute path
Call GetAllFile(SFN) 'Call recursion
End If
Next
dim strFlName
'************Vide to the file**********
For Each OneFile In objFf
strFlName=OneFile.Name
'desktop.ini and folder.htt are not in the column range
If strFlName<>"desktop.ini" EQV strFlName<>"folder.htt" Then
FN=Folder&""&strFlName
Counter=Counter+ColorOn(FN)
End If
Next
'******************************
'Close each object instance
Set objFd=Nothing
Set objFs=Nothing
Set objFf=Nothing
End Function
'*************************** Generate matching pattern******************************************
Private Function CreatePattern(keyword)
CreatePattern=keyword
CreatePattern=Replace(CreatePattern,".",".")
CreatePattern=Replace(CreatePattern,"+","+")
CreatePattern=Replace(CreatePattern,"(","(")
CreatePattern=Replace(CreatePattern,")",")")
CreatePattern=Replace(CreatePattern,"[","[")
CreatePattern=Replace(CreatePattern,"]","]")
CreatePattern=Replace(CreatePattern,"{","{")
CreatePattern=Replace(CreatePattern,"}","}")
CreatePattern=Replace(CreatePattern,"*","[^//]*") '* number matches
CreatePattern=Replace(CreatePattern,"?","[^//]{1}") '? number matching
CreatePattern="("&CreatePattern&")+" 'Overall Match
End Function
'****************************** Search and color keywords ****************************
Private Function ColorOn(FileName)
dim objReg
Set objReg=new RegExp
objReg.Pattern=CreatePattern(keyword)
objReg.IgnoreCase=True
objReg.Global=True
retVal=objReg.Test(FileName) 'Check the search test, if it passes, color and output
if retVal then
OutPut=objReg.Replace(FileName,"<font color='#FF0000'>$1</font>") 'Set the display color of the keyword
'****************************** This part can modify the output as needed******************************************************
OutPut="<a href='#'>"&OutPut&"</a><br/>"
Response.Write(OutPut) 'Output matching result
'*********************************************************************
ColorOn=1 'Number of added counters
else
ColorOn=0
end if
Set objReg=Nothing
End Function
End Class
'****************************** End class SearchFile*********************
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>www.csdn.com.cn</title>
</head>
<body>
<form name="form1" method="post" action="<% =Request.ServerVariables("PATH_INFO")%>">
Keywords:
<input name="keyword" type="text" id="keyword">
<input type="submit" name="Submit" value="Search">
<a href="help.htm" target="_blank">Advanced Search Help</a>
</form>
<%
dim keyword
keyword=Request.Form("keyword")
if keyword<>"" then
Set newssearch=new SearchFile
newsearch.Folders="E:Media+F:"
newsearch.keyword=keyword
newssearch.Search
Set newsearch=Nothing
response.Write("<br/>Time-consuming: "&(timer()-st)*1000&"ms")
end if
%>
</body>
</html>
If you think this FSO method to search hard disk files is helpful to you, please give me a thumbs up. If you want to view more related content, please follow the WoNew Technology Channel. Thank you for your support!