VB 使用 Dir 函数遍历文件夹
来源:互联网 发布:JS确认框怎么设置 编辑:程序博客网 时间:2024/06/03 14:26
Dir[(pathname[, attributes])]
Dir 函数的语法具有以下几个部分:
以 vbDirectory 属性来调用 Dir 不能连续地返回子目录。!!!
以下是VB帮助自带查目录的例子,明确的指出目录必须要用GetAttr!!!
' 显示 C:\ 目录下的名称。
MyPath = "c:\" ' 指定路径。
MyName = Dir(MyPath, vbDirectory) ' 找寻第一项。
Do While MyName <> "" ' 开始循环。
' 跳过当前的目录及上层目录。
If MyName <> "." And MyName <> ".." Then
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' 如果它是一个目录,将其名称显示出来。
End If
End If
MyName = Dir ' 查找下一个目录。
Loop
=========================================
用VB函数Dir实现递归搜索目录
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'函数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
- VB 使用 Dir 函数遍历文件夹
- vb.net 使用 DIR 遍历文件目录
- 使用Dir函数遍历文件和目录
- dir函数:遍历文件名
- vb中dir函数用法
- VB遍历目录文件夹2
- VB 分别用DIR和API遍历驱动器实例
- VB遍历某目录下的某类型文件(Dir)
- vb 遍历文件夹 与遍历文件
- vb 遍历文件夹 与遍历文件
- c++ 遍历文件夹函数
- 用VB函数Dir实现递归搜索目录
- 用VB函数Dir实现递归搜索目录(转载)
- 使用list.files函数遍历文件夹中的文件
- 使用递归遍历文件夹
- matlab中dir()函数的使用
- dir函数
- dir函数
- Nosql --MongoDB 一些基础语法
- 杭电ACM 1175
- 音视频编码基本概念
- 登陆服务器老出现“达到最大连接数"解决方法
- ndk开发打包.so文件到apk包的方法
- VB 使用 Dir 函数遍历文件夹
- Joda-Time 简介
- android VelocityTracker类
- jQuery中的CheckBox、RadioButton、DropDownList的取值赋值
- Linux 2.6下Driver开发的34个变化[转贴]
- 最实用的GDI辅助类
- 基于Hadoop平台的并行数据挖掘算法工具-Dodo
- AsyncSocket发送数据错误
- 高通89系列的fastboot烧写顺序