<%
data=request.form("search_data")
' 從FORM變量集得到要查詢的文件名稱所要包含的字符串
p=search_folder(data,"http://jack/cgi-bin","c:\intels\wwwroot\cgi-bin")
' 調用函數查詢目標查詢目錄下的所有子目錄(所有子樹),方法:search_folder(要查詢的字符串,查詢目標的虛擬絕對路徑,查詢目標的真實絕對路徑)
%>
<script language="vbscript" RUNAT=SERVER>
function search_folder(search_data,v_path,c_path)
dim file_system,cur_folder,sub_folders,sub_files
' 目錄檢索函數
if not isempty(search_data) and len(search_data)>0 then
' 確定查詢字符串有效非空
set file_system=createobject("scripting.filesystemobject")
' 建立文件系統對象
set cur_folder=file_system.getfolder(c_path)
' 建立建立當前目錄對象
set sub_folders=cur_folder.subfolders
' 建立當前目錄的子目錄對象集合
for each each_sub_folder in sub_folders
' 對子目錄集合進行遍歷
if each_sub_folder.attributes=16 then
' 確定子目錄的屬性為普通子目錄
sub_v_path=v_path&"/"&each_sub_folder.name
sub_c_path=c_path&""&each_sub_folder.name
'得到當前的子虛擬絕對路徑與真實絕對路徑,默認子目錄與子虛擬目錄級別名稱完全相同
p=search_file(search_data,sub_v_path,sub_c_path)
' 調用文件檢索函數對當前子目錄下的文件進行字符串匹配檢索
p=search_folder(search_data,sub_v_path,sub_c_path)
' 遞歸檢索當前子目錄的下一級目錄
end if
next
set each_sub_folder=nothing
set sub_folders=nothing
set cur_folder=nothing
set file_system=nothing
' 清空
end if
end function
--------------------------------------------------------------------------------------------------------------------------------
function search_file(search_data,v_path,c_path)
dim file_system,sub_files,sub_file_v_path,sub_out_v_path
' 文件匹配檢索函數
if not isempty(c_path) then
' 確認路徑不為空
set file_system=createobject("scripting.filesystemobject")
set cur_folder=file_system.getfolder(c_path)
set sub_files=cur_folder.files
' 建立檢索目錄下的文件對象集合
for each each_file in sub_files
' 遍歷文件對象集合
if instr(each_file.name,search_data)<>0 then
' 匹配字符串與文件名
sub_file_v_path=v_path&"/"&each_file.name
' 建立可用鏈接,輸出匹配文件
sub_out_v_path=Replace(sub_file_v_path," ","%20")
' 替換路徑及文件名中的空格,以便系統確定路徑
response.write("<p><a
href="&sub_out_v_path&">"&sub_file_v_path&"</a>")
end if
next
set sub_out_v_path=nothing
set sub_file_v_path=nothing
set each_file=nothing
set sub_files=nothing
set file_system=nothing
end if
end function
</script>