您当前的位置:首页 > 计算机 > 编程开发 > VB

VB编程:获取文件夹下所有文件

时间:05-25来源:作者:点击数:

VB编程:获取文件夹下所有文件

Private Function TreeSearch(ByVal sPath As String, ByVal sFileSpec As String) As Long
    DoEvents
    Static Files As Long
    Dim sDir As String
    Dim sSubDirs() As String
    Dim Index As Long
    Dim sFiles() As String
    
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    '获取文件名和数目
    
    sDir = Dir(sPath & sFileSpec)
    
    Do While Len(sDir)
    
       Files = Files + 1
       ReDim Preserve sFiles(1 To Files)
       sFiles(Files) = sPath & sDir
       
       '显示到列表
       List1.AddItem sFiles(Files)
       List1.ListIndex = List1.ListCount - 1
       
       sDir = Dir
    Loop
    '获取文件夹名称
    Index = 0
    sDir = Dir(sPath, vbDirectory)
    Do While Len(sDir) 'sDir <> ""
        If sDir <> "." And sDir <> ".." Then
            If GetAttr(sPath & sDir) And vbDirectory Then
                Index = Index + 1
                ReDim Preserve sSubDirs(1 To Index)
                sSubDirs(Index) = sPath & sDir & "\"
            End If
        End If
        sDir = Dir
    Loop
    '递归调用,获取子文件夹目录
    For Index = 1 To Index
        Call TreeSearch(sSubDirs(Index), sFileSpec)
    Next Index
    TreeSearch = Files
End Function
Private Sub Command1_Click()
    
    Print TreeSearch(Text1.Text, Combo1.Text)
    
End Sub
Private Sub Command2_Click()
    End
End Sub


'Private Sub Command3_Click()
'    List1.Clear
'End Sub
Private Sub Form_Load()
    Text1.Text = "D:\vb小程序\vb实例"
    Dim spec As Variant
    spec = Array("*.*", "*.exe", "*.vbp")
    Combo1.Text = spec(0)
    For i = 0 To UBound(spec)
        Combo1.AddItem spec(i)
    Next i
End Sub

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门