您当前的位置: 首页 >  彭世瑜

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

彭世瑜 发布时间:2017-03-10 18:24:53 ,浏览量:2


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

关注
打赏
查看更多评论

彭世瑜

暂无认证

  • 2浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录