visualbasic6.0遍歷文件夾下所有文件代碼

字號(hào):

經(jīng)常需要的代碼,考試大為您整理!
    ’sPath是所查找的文件夾的路徑,list是返回的文件列表
    Public Function GetAllFiles(ByVal sPath As String, list As Collection)
    Dim item As String
    Dim oPaths As New Collection
    item = Dir(sPath, vbDirectory)
    While Len(item) > 0
    If item <> "." And item <> ".." Then
    If (GetAttr(FullPathName(sPath) & item) And vbDirectory) = vbDirectory Then
    oPaths.Add item
    Else
    If IsModelFile(item) Then list.Add sPath & item
    End If
    End If
    item = Dir
    Wend
    Dim p
    For Each p In oPaths
    Call GetAllFiles(Combin(sPath, p), list)
    Next
    End Function
    ’判斷文件夾后是否有\(zhòng)
    Public Function FullPathName(ByVal strPath As String) As String
    FullPathName = IIf(VBA.Right(strPath, 1) = "\", strPath, strPath & "\")
    End Function
    ’多個(gè)文件夾名組成路徑
    Public Function Combin(ParamArray arg()) As String
    Dim X
    Dim result As String
    For Each X In arg()
    result = result & FullPathName(X)
    Next
    Combin = result
    End Function