怎么列表指定目录下的文件(急)

gongfucai 2003-11-24 02:43:37
help
...全文
36 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihonggen0 2003-11-24
  • 打赏
  • 举报
回复
1. DIR

Get a sorted list of files in a directory using Dir and Quicksort
http://www.vb-helper.com/howto_dir_quicksorted.html

2. FSO

Get a sorted list of files in a directory using the File System Object (FSO) and Quicksort
http://www.vb-helper.com/howto_dir_fso.html

3. API
Minimal Code for a Recursive Search for Files (single drive)
http://www.mvps.org/vbnet/index.html?code/fileapi/recursivefiles_minimal.htm
lihonggen0 2003-11-24
  • 打赏
  • 举报
回复
下面这个递归算法读取一个文件夹(包括子文件夹)中的所有文件

Private Sub Command1_Click()
Dim ff() As String
Dim fn As Long
Dim i As Long

fn = TreeSearch("c:\aaa", "*.txt", ff())

Debug.Print "找到文件数目为" & fn
For i = 1 To fn
Debug.Print ff(i)
Next
End Sub


Private Function TreeSearch(ByVal sPath As String, ByVal sFileSpec As String, sFiles() As String) As Long
Static lngFiles As Long
Dim lngIndex As Long
Dim strDir As String
Dim strSubDirs() As String

If Right(sPath, 1) <> "\" Then
sPath = sPath & "\"
End If

strDir = Dir(sPath & sFileSpec)
Do While Len(strDir)
lngFiles = lngFiles + 1
ReDim Preserve sFiles(1 To lngFiles)
sFiles(lngFiles) = sPath & strDir
strDir = Dir
Loop

lngIndex = 0
strDir = Dir(sPath & "*.*", 16)

Do While Len(strDir)
If Left(strDir, 1) <> "." Then
If GetAttr(sPath & strDir) And vbDirectory Then
lngIndex = lngIndex + 1
ReDim Preserve strSubDirs(1 To lngIndex)
strSubDirs(lngIndex) = sPath & strDir & "\"
End If
End If
strDir = Dir
Loop

For lngIndex = 1 To lngIndex
Call TreeSearch(strSubDirs(lngIndex), sFileSpec, sFiles())
Next lngIndex

TreeSearch = lngFiles
End Function


lihonggen0 2003-11-24
  • 打赏
  • 举报
回复
利用Dir函数:参考下面的例子

Dim MyFile, MyPath, MyName

' 返回带指定扩展名的文件名。如果超过一个 *.txt 文件存在,
' 函数将返回按条件第一个找到的文件名。
MyFile = Dir("C:\WINDOWS\*.txt")

' 若第二次调用 Dir 函数,但不带任何参数,则函数将返回同一目录下的下一个 *.txt 文件。
MyFile = Dir

' 返回找到的第一个隐式 *.TXT 文件。
MyFile = Dir("*.TXT", vbHidden)

' 显示 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

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧