VB6 用API 函数列出指定文件夹中的所有文件

Private Const MAX_PATH = 260
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
End Type
Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
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


Public Function All_Files(Dir As String, List() As String) As Boolean
    If SetCurrentDirectory(Dir) Then
        Dim Yes As Boolean
        Dim hFindFile As Long
        Dim F As WIN32_FIND_DATA, ClearF As WIN32_FIND_DATA
        Dim FileCount As Integer: FileCount = 0
        ReDim List(1 To 1) As String
        If Right(Dir, 1) <> "\" Then Dir = Dir & "\"
        hFindFile = FindFirstFile(Dir & "*.*", F)
        If hFindFile > 0 Then
            Do
                If F.dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY Then
                    FileCount = FileCount + 1
                    ReDim Preserve List(1 To FileCount) As String
                    List(FileCount) = Dir & F.cFileName
                    All_Files = True
                End If
                F = ClearF
                Yes = FindNextFile(hFindFile, F)
            Loop While Yes
        Else
            All_Files = False
        End If
        FindClose hFindFile
    Else
        All_Files = False
    End If
End Function


Private Sub Command1_Click()
    Dim List() As String
    If All_Files("C:\Files\", List) Then
        For i = 1 To UBound(List)
            Debug.Print List(i)
        Next
    End If
End Sub

你可能感兴趣的:(VB6.0)