专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »VB教程 » vbdir函数:利用VB函数Dir()实现递归搜索目录 »正文

vbdir函数:利用VB函数Dir()实现递归搜索目录

来源: 发布时间:星期四, 2009年1月15日 浏览:9次 评论:0
  我在很久以前就实现了这个思路方法了它没有采用任何Control控件形式也没有系统APIFindFirst,FindNext进行递归和别人有点区别就是我用是VB中Dir事实上直接采用Dir是不能进行自身递归但我们可以采用种办法把Dir将当前搜索目录子目录给保存下来然后在自身search(strPathName)递归中依次进行递归这样就可以把指定目录搜索完毕

  具体代码如下:

  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'GetExtName
'功能:得到文件后缀名(扩展名)
'输入:文件名
'输出:文件后缀名(扩展名)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function GetExtName(strFileName As String) As String
 Dim strTmp As String
 Dim strByte As String
 Dim i As Long
 For i = Len(strFileName) To 1 Step -1
  strByte = Mid(strFileName, i, 1)
  If strByte <> "." Then
   strTmp = strByte + strTmp
  Else
   Exit For
  End If
 Next i
 GetExtName = strTmp
End Function
Public Function search(ByVal strPath As String, Optional strSearch As String = "") As Boolean
 Dim strFileDir As String
 Dim strFile As String
 Dim i As Long
 Dim lDirCount As Long
 On Error GoTo MyErr
 If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
 strFile = Dir(strPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
 While strFile <> "" '搜索当前目录
 DoEvents
  If (GetAttr(strPath + strFile) And vbDirectory) = vbDirectory Then '如果找到是目录
   If strFile <> "." And strFile <> ".." Then '排除掉父目录(..)和当前目录(.)
    lDirCount = lDirCount + 1 '将目录数增1
    ReDim Preserve strFileDir(lDirCount) As String
    strFileDir(lDirCount - 1) = strFile '用动态保存当前目录名
   End If
  Else
   If strSearch = "" Then
    Form1.List1.AddItem strPath + strFile
   ElseIf LCase(GetExtName(strPath + strFile)) = LCase(GetExtName(strSearch)) Then
    '满足搜索条件则处理该文件
    Form1.List1.AddItem strPath + strFile '将文件全名保存至列表框List1中
   End If
  End If
  strFile = Dir
 Wend
 For i = 0 To lDirCount - 1
  Form1.Label3.Caption = strPath + strFileDir(i)
  Call search(strPath + strFileDir(i), strSearch) '递归搜索子目录
 Next
 ReDim strFileDir(0) '将动态清空
 search = True '搜索成功
 Exit Function
MyErr:
 search = False '搜索失败
End Function


0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: