你也许想:好,现在我知道如何写入文件了。但能做到更多一些吗?下面来试一试为web站点建立一个搜索功能。建立搜索引擎的关键是递归。主要地,编写一段代码搜索目录下的文件,然后对所有的目录循环执行同样的代码。因为不能确定总共有多少个子目录,所以必须一遍又一遍地执行搜索代码,直到结束。递归调用非常好!下面来创建搜索页面。假设已经建立了一个HTML表单,用户在其中输入一个搜索字符串。
; @$ l. w/ p; a% ^
, v; v. K4 y7 _2 c' l, u# GDim objFolder
) {% `( d {4 V9 a4 ]9 m% BDim strSearchText" }$ R2 v% `& P- X, B
Dim objFSO, q/ m; J- x' y/ _1 l$ N7 T H
* i3 U) H# s9 Q xstrSearchText = Request.Form(\"SearchText\") < -- The search string) E: W3 l9 v0 V3 R. S
\' create the FSO and Folder objects$ e6 {' W9 s& e7 x; G7 P) u# A" y
Set fso = Server.CreateObject(\"Scripting.FileSystemObject\")
- s# }; ]% s* }3 a0 g% k" {( xSet objFolder = objFSO.GetFolder(Server.MapPath(\"/\"))
! H/ p" E4 j8 O" m- L7 G7 Y f9 g7 j8 v6 }2 R. e
Search objFolder
7 `0 V& Y! e2 L) i4 |' W9 c9 x4 M/ L4 t9 W* i( i
" @ [ O- ]9 h: ]- c, s
上面的代码简单地初始化变量,Search函数执行搜索功能,描述如下:
6 V6 x1 {& ~( ~4 r* _" R
- a+ Z8 ?! w; {* `+ X
9 N% V8 T4 }- S2 R
) C( d8 z% M) S! _, M8 X& GFunction Search(objFolder)
: ~* t( m. T: ^7 L6 e1 H/ f2 T5 o) @7 l- s
Dim objSubFolder2 [" u/ B) U0 r
" ]( h2 M' n. j9 q) o6 n3 a) o+ K( F' A! d3 u# v) d Z
+ N! Z) R6 l! A% j% ]9 g \'loop through every file in the current
, G! g! A- O4 m. _8 C$ E5 B; Jfolder
- ^8 }4 j7 @8 ]4 V& C/ w" {- ]0 S' t4 O, }8 f, w( H: E
For Each objFile in objFolder.Files
8 o! w0 J2 G. \0 {! h9 t6 D+ R# n4 C) G! z3 g Q! K q
Set objTextStream = objFSO.OpenTextFile(objFile.Path,1) < -- For Reading
& P/ R ?( k1 p" V+ O* U x# d" i) q- x; b. W5 _
) [- I8 b8 v! S
2 F$ f t- t& [: P3 W6 x \'read the file\'s contents into a
. X j8 T" J2 ?8 A, N/ n% Nvariable
" x- }, i9 q: u, x) V& L6 ]
5 [/ l- n7 D3 _ E2 o! a strFileContents = objTextStream.ReadAll' o! c/ S, @4 |1 G) d8 W5 `5 J
) O! ?: J) }$ L4 w
' F& ]6 K/ i- u) ^' ^( u' }3 L2 A/ L4 d6 D7 B$ P, G
\'if the search string is in the file, then
. `6 T0 F; S+ Fwrite a link5 y# \" C& f% e+ A! R+ f2 N, R
7 r- A* I7 w' F( l' q- v
\' to the file* H; s6 v" C2 r
8 g) Q5 w' t* u+ J If InStr(1, strFileContents, strSearchText, 1) then) k7 t0 @% Q/ z- k. o; X( ^
& [+ [2 I8 {* Y$ J1 l' e- g6 j Response.Write \"< A HREF=\"\"/\" & objFile.Name & _5 F( o* T: {' ^4 N! A' T) t
: Q: G% R$ C" M \"\"\">\" & objFile.Name & \"< /A>< BR>\"
7 i# s% A. g- W% d2 P9 l+ s+ U4 ?* \. T t
' |5 d- x; X0 U: d6 O" t$ P, L6 J% r1 j% Z" C' e: J
bolFileFound = True
0 _1 Y: H1 x2 _0 T' ~/ g( c) n. I! D" ^+ S
End If3 a. p9 H5 P3 G9 C/ @0 w
; _; M( X) m: A
7 p+ l( {, H8 M. @. X2 i2 j1 ]) |* H1 r; a; M+ C/ }
objTextStream.Close# M% E4 P2 S. {, M& h- s
' x/ r1 t% }8 [
% j' X7 c6 i; h, R, J/ o) y
6 ~: k1 s! q' t2 x' d
Next6 G6 ?* u# i7 p; [8 K7 S& a
$ P' K+ Y7 Q+ ?) }* R$ E) v6 v9 h# ]- |2 k& S
- E ?: B: F- X+ G
\'Here\'s the recursion part - for each: x$ W7 P D% c' K3 }! _% s
1 c1 K7 e9 Y& y" c+ A8 L
\' subfolder in this directory, run the Search function again
! R$ S2 T( t( I8 n% r
' o% N) p4 X/ n% J1 y5 N For Each objSubFolder in objFolder.SubFolders5 b6 d* D) o0 S; I ^
) X" m/ J5 d, j, Z+ B5 [ Search objSubFolder
6 l& G/ X' v8 o
/ A. d4 Z0 W* i9 g7 `& Y \+ U8 } Next
1 B# E2 [3 a% g& ~& ?' |
9 b6 g& Q2 f" ^* T% k% z- b8 i8 uEnd Function
1 |0 t( g; ^2 z0 p) ^$ F" A' c
3 e) @$ G0 I- ^ U& h. Y% x: Q. W 为了能打开文件,FSO需要实际的文件路径,而不是web路径。比如,是c:inetpubwwwroot empindex.html, 而不是 ) B- _ s; M& Z1 p2 E4 R. h. t
www.enfused.com/temp/index.html 或者 /temp/index.html。 为了将后者转换为前者,使用Server.MapPath
* }. u% n% p9 F5 i' x$ T8 ~(\"filename\"), filename表示web路径名。
- b1 @, J7 ?' L/ {+ W; |+ D5 u+ a8 X N! R7 K; p0 G. N: e. D: p( D
上面的代码将在你指定的初始目录下的文件夹的每一个子目录中执行,在这里,初始目录是指web根目录“/”。然后+ b7 L1 H$ }! i' f/ N" b% [$ u
就简单地打开目录下的每一个文件,看看其中是否包含指定的字符串,如果找到字符串就显示那个文件的链接。! W3 v- r8 h0 `1 f; [2 c: z* w
! ^7 \$ r" r* g
注意,随着文件和子目录数量的增加,搜索花费的时间也将增加。如果需要繁重的搜索工作,建议你采取其他的方
( |# }" e7 b" N4 S* y) j- K( `法,比如微软公司的索引服务器Index Server。 |