VB遍历目录文件夹2

Private Sub Command1_Click()
get_folders List1, "c:\windows"
End Sub

Private Sub get_folders(ByRef List1 As ListBox, ByVal path As String)
'引用了Microsoft Scripting Runtime
Dim fso As New Scripting.FileSystemObject
Dim fd As Scripting.Folder
Dim fd_1 As Scripting.Folder
Set fd = fso.GetFolder(path)
For Each fd_1 In fd.SubFolders
List1.AddItem fd_1.path
Next
End Sub

'程序代码 '遍历文件夹
Private Sub ShowFolderList(folderspec)
     Dim fs, f, f1, s, sf
     Dim hs, h, h1, hf
     Set fs = CreateObject("Scripting.FileSystemObject")
     Set f = fs.GetFolder(folderspec)
     Set sf = f.SubFolders
     For Each f1 In sf
        
     List1.AddItem folderspec & "\" & f1.Name
    
           Call ShowFolderList(folderspec & "\" & f1.Name)
     Next
End Sub

'程序代码 '遍历某文件夹下的文件
Private Sub Showfilelist(folderspec)
     Dim fs, f, f1, fc, s
     Set fs = CreateObject("Scripting.FileSystemObject")
     Set f = fs.GetFolder(folderspec)
     Set fc = f.Files
     For Each f1 In fc
     List1.AddItem f1.Name
     Next
End Sub

'程序代码 '遍历某文件夹及子文件夹中的所有文件
Sub sosuofile(MyPath As String)
Dim Myname As String
Dim a As String
Dim B() As String
Dim dir_i() As String
Dim i, idir As Long
If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While Myname <> ""
If Myname <> "." And Myname <> ".." Then
If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
idir = idir + 1
ReDim Preserve dir_i(idir) As String
dir_i(idir - 1) = Myname
Else

List1.AddItem MyPath & Myname   '把找到的文件显示到列表框中

End If
End If
Myname = Dir '搜索下一项
Loop
For i = 0 To idir - 1
Call sosuofile(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String
       '在这里可以处理目录中的文件
       'Fn.Name       '得到文件名
       'Fn.Size       '得到文件大小
       'Fn.Path       '得到文件路径
       'Fn.Type       '得到文件类型
       'Fn.DateLastModified       '得到文件最后的修改日期
End Sub

Private Sub Command1_Click()
ShowFolderList "c:\windows"
End Sub

Private Sub Command2_Click()
Showfilelist "c:\windows"
End Sub


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
VB中,可以使用API函数来遍历指定驱动器或目录的文件。首先,我们需要声明一些需要使用的API函数。 1. 声明函数:需要使用FindFirstFile、FindNextFile和FindClose函数来进行文件搜索。 Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long 2. 声明数据结构:需要使用WIN32_FIND_DATA结构体来存储搜索到的文件信息。 Private Type WIN32_FIND_DATA dwFileAttributes As Long ftCreationTime As Currency ftLastAccessTime As Currency ftLastWriteTime As Currency nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName As String * 260 cAlternate As String * 14 End Type 接下来,我们可以编写一个过程来进行文件搜索并遍历。 Private Sub GetFiles(ByVal path As String) Dim hSearch As Long Dim findData As WIN32_FIND_DATA '添加路径分隔符 If Right$(path, 1) <> "\" Then path = path & "\" End If '开始搜索 hSearch = FindFirstFile(path & "*", findData) If hSearch <> -1 Then Do '忽略隐藏和系统文件夹 If (findData.dwFileAttributes And vbHidden) = 0 And (findData.dwFileAttributes And vbSystem) = 0 Then '找到了文件,处理文件 Debug.Print findData.cFileName End If Loop While FindNextFile(hSearch, findData) <> 0 FindClose hSearch End If End Sub 在这个过程中,我们首先根据路径和通配符 "*" 调用FindFirstFile函数来搜索第一个文件,然后使用FindNextFile函数来继续搜索下一个文件,直到没有文件可搜索为止。在每一次搜索到文件时,我们可以进行相应的处理,这里只是简单地输出文件名。 例如,我们可以调用以下代码来遍历D盘根目录的文件。 GetFiles "D:\" 这样就可以遍历D盘根目录的所有文件,并输出文件名。当然,你可以根据自己的需求对搜索到的文件进行处理。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值