VB获得文件夹及其子文件夹下所有文件名

snr278 2010-08-06 01:16:06
如题,刚学VB没有多久,请各位大大们帮帮忙
...全文
639 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
snr278 2010-08-06
  • 打赏
  • 举报
回复
非常谢谢
嗷嗷叫的老马 2010-08-06
  • 打赏
  • 举报
回复
我.........算了........给你代码吧-_-b

先在宏编辑器里添加一个模块,然后把下面的内容复制过去:

Option Explicit
'*************************************************************************
'**模 块 名:ModSearchFile
'**说 明:搜索文件
'**创 建 人:嗷嗷叫的老马
'**日 期:2004年10月27日
'**版 本:V1.0
'*************************************************************************

Private FoundFile() As String '存放传回值的字串阵列
Private Ntx As Long

Public Function SearchFileInPath(ByVal thePath As String, ByVal theFileName As String, Optional ByVal mStop As Boolean = False) As String()
'使用递归方式搜索文件
'thePath - 要搜索的目录
'theFileName - 文件名,支持通配符
'mStop - T=找到一个就返回,F=返回所有找到的文件
'返回值:
' 搜索到的文件
If Right(thePath, 1) <> "\" Then thePath = thePath & "\"
Call GetFileLoop(thePath, theFileName, mStop)
SearchFileInPath = FoundFile
End Function

Private Function GetFileLoop(CurrentPath As String, ByVal SearFile As String, Optional ByVal mStop As Boolean = False) As String
Dim nI As Integer, nDirectory As Integer, I As Long
Dim sFileName As String, sDirectoryList() As String

On Error Resume Next
sFileName = Dir(CurrentPath, vbHidden Or vbDirectory Or vbReadOnly Or vbSystem)
Do While sFileName <> ""
If UCase(sFileName) Like UCase(SearFile) Then
I = GetAttr(CurrentPath + sFileName)
If (I And vbDirectory) = 0 Then
If mStop = False Then
ReDim Preserve FoundFile(Ntx)
FoundFile(Ntx) = CurrentPath + sFileName
Ntx = Ntx + 1
Else
GetFileLoop = CurrentPath + sFileName
Exit Function
End If
End If
End If
If sFileName <> "." And sFileName <> ".." Then
If GetAttr(CurrentPath & sFileName) _
And vbDirectory Then

nDirectory = nDirectory + 1
ReDim Preserve sDirectoryList(nDirectory)
sDirectoryList(nDirectory) = CurrentPath & sFileName
End If
End If
sFileName = Dir
Loop
For nI = 1 To nDirectory
GetFileLoop = GetFileLoop(sDirectoryList(nI) & "\", SearFile)
If GetFileLoop <> "" And mStop = True Then Exit For
Next nI
End Function

接着就可以调用了.

方法是,传入要搜索的目录,以及要搜索的文件的过滤器,然后就会返回一个数组,里面是找到的文件名.
snr278 2010-08-06
  • 打赏
  • 举报
回复
虽然很简单,但还是请帮忙答一下啊,自己顶一下
snr278 2010-08-06
  • 打赏
  • 举报
回复
先谢谢了,但是能不能请你再说明白点,这个模块到底怎么加进去啊,加什么模块啊!
嗷嗷叫的老马 2010-08-06
  • 打赏
  • 举报
回复
还不是一样.

把模块添加进去.
snr278 2010-08-06
  • 打赏
  • 举报
回复
看来我有些弄混了,我是在excel里面编宏,这个好像不能用啊,excel和VB还是不同的啊
snr278 2010-08-06
  • 打赏
  • 举报
回复
这个能在excel里面用吗
嗷嗷叫的老马 2010-08-06
  • 打赏
  • 举报
回复
参考:

DIR+递归实现搜索文件,支持通配符(VB6.0)

Sub Test()
'搜索文件模块测试过程
'BY 嗷嗷叫的老马
'紫水晶工作室
'http://www.m5home.com/
Dim I() As String, J As Long

I = SearchFileInPath("c:\windows\web", "*.*")
For J = 0 To UBound(I)
Debug.Print I(J) '打印所有文件
Next
End Sub

7,765

社区成员

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

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