^_^---心靜就是好命---^_^

利用递归搜索子目录

2008-02-02 13:48:45 / 个人分类:VB學習

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''VBGood社区门户5ie` pr/E
'函数GetExtNameVBGood社区门户a"~/`H:{By
'功能:得到文件后缀名(扩展名)
S5SC+sE:D$H0'输入:文件名
2r,Q(TM JV(X(RZ0'输出:文件后缀名(扩展名)VBGood社区门户7Ow#sg!y VmQ"F
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function GetExtName(strFileName As String) As StringVBGood社区门户)qvx6xz6a
     Dim strTmp As StringVBGood社区门户mKHEi;Fj'm
     Dim strByte As StringVBGood社区门户:F`+d5H7yy(|F
     Dim i As Long
C*m Fy&A c3n]0     For i = Len(strFileName) To 1 Step -1
uB*A:D.Uwe w`\*} Ty0      strByte = Mid(strFileName, i, 1)VBGood社区门户6l0O6} {"H|)mxXs*y
      If strByte <> "." ThenVBGood社区门户4gO'LIjkF
       strTmp = strByte + strTmp
0[q4x0q)x$j }f0      Else
9Bn*L!@ B4B0       Exit For
}?rA DR}o1V$s)a0      End IfVBGood社区门户ZNi3k{.kc
     Next iVBGood社区门户,[gM/{Rk;nh/C
     GetExtName = strTmp
q"zQ"h}3l%`N+k0H0End Function


l4L8Qx1O@Y{0Public Function search(ByVal strPath As String, Optional strSearch As String = "") As Boolean
!AW{;lL$ku0     Dim strFileDir() As String
V2[3mCu't0     Dim strFile As StringVBGood社区门户8d$_l0`lt
     Dim i As Long
wb2Nm ^Z8X-wu0     Dim lDirCount As LongVBGood社区门户 zC{w!H:T'^
     On Error GoTo MyErr
*CI.r_N JA!IRB0     If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
t"g0] I1dk7I"Q0     strFile = Dir(strPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
y8V'g` w!R$c0     While strFile <> "" '搜索当前目录
t$A)Xh;J0     DoEventsVBGood社区门户B,j[)hH,eV}
      If (GetAttr(strPath + strFile) And vbDirectory) = vbDirectory Then '如果找到的是目录
-z VDh*T3UNa ~0       If strFile <> "." And strFile <> ".." Then '排除掉父目录(..)和当前目录(.)
,kC*W{K \:o0        lDirCount = lDirCount + 1 '将目录数增1
;xcD5{1N#AtVm0        ReDim Preserve strFileDir(lDirCount) As StringVBGood社区门户.S5zR;g-n;ccm5S
        strFileDir(lDirCount - 1) = strFile '用动态数组保存当前目录名VBGood社区门户nr2S1pj`"J7UC
       End IfVBGood社区门户%U-Jg;U(Y] |*]G)u
      ElseVBGood社区门户Lv1h9|8i8o
       If strSearch = "" Then
1C8Tq;[9h ?x4v0        Form1.List1.AddItem strPath + strFileVBGood社区门户#CH^(vpZ'^$i$vS
       ElseIf LCase(GetExtName(strPath + strFile)) = LCase(GetExtName(strSearch)) ThenVBGood社区门户Afh/Af7KA.?8N4D
        '满足搜索条件,则处理该文件
8X*]Y,oO e R3]0        Form1.List1.AddItem strPath + strFile '将文件全名保存至列表框List1中
$p-a)~@g9ENd9z.AY0j9}0       End If
s+?t_3fz)e3o0      End If
/\Z$pm5^e0t[0      strFile = Dir
/{1g:H5E3^X:RlM+M(r0     Wend
8e*^ V%K| };H:J5h["|0     For i = 0 To lDirCount - 1
)C8Ql QF#af8V0      Form1.Label3.Caption = strPath + strFileDir(i)VBGood社区门户}Uu` G RZ
      Call search(strPath + strFileDir(i), strSearch) '递归搜索子目录
C7K&__5I/KsG0     NextVBGood社区门户3[2}9Yd7NQ m$[
     ReDim strFileDir(0) '将动态数组清空VBGood社区门户)ZE$h8b*hM
     search = True '搜索成功VBGood社区门户d#yk\A+o
     Exit FunctionVBGood社区门户0b ooU U+[Y L
MyErr:VBGood社区门户Ae Ff:Ql-b
     search = False '搜索失败VBGood社区门户Q+P\ VT9mp"m/c1R
End Function

 

TAG: VB學習

我来说两句

-5 -3 -1 - +1 +3 +5

Open Toolbar