如何获得一个目录下的所有子文件名称(包括子文件下的子文件名)?谢谢了。

解决方案 »

  1.   

    Option Explicit Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
    (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
    (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As LongPublic Const MAX_PATH = 260
    Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Public Const FILE_ATTRIBUTE_COMPRESSED = &H800
    Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Public Const FILE_ATTRIBUTE_HIDDEN = &H2
    Public Const FILE_ATTRIBUTE_NORMAL = &H80
    Public Const FILE_ATTRIBUTE_READONLY = &H1
    Public Const FILE_ATTRIBUTE_SYSTEM = &H4
    Public Const FILE_ATTRIBUTE_TEMPORARY = &H100'自定义数据类型FILETIME和WIN32_FIND_DATA的定义
    Public Type FILETIME
     dwLowDateTime As Long
     dwHighDateTime As Long
    End TypePublic 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
    ----------------------
    '--------------------------------------------------------------------------------
    ' 把当前文件夹路径下的所有文件入到listview中
    '--------------------------------------------------------------------------------
    Private Sub finfiles(tCurrentdir As String)
     Dim itmX As ListItem
     Dim tFindData As WIN32_FIND_DATA
     Dim strFileName As String
     Dim lHandle As Long
     Dim CountFolder As Integer
     Dim CountFiles As Integer
     CountFolder = 0
     CountFiles = 0
     ListView1.ListItems.Clear
     lHandle = FindFirstFile(tCurrentdir & "\*.*", tFindData)
     If lHandle = 0 Then
      Set itmX = ListView1.ListItems.Add(, , strFileName & "找不到文件")
      Exit Sub
     End If
     strFileName = fDelInvaildChr(tFindData.cFileName)
     Do While True
      tFindData.cFileName = ""
      If FindNextFile(lHandle, tFindData) = 0 Then
       FindClose (lHandle)
       Exit Do
      Else
       strFileName = fDelInvaildChr(tFindData.cFileName)
       If tFindData.dwFileAttributes = &H10 Then
        If strFileName <> "." And strFileName <> "." Then
         Set itmX = ListView1.ListItems.Add(, , strFileName)
         itmX.SmallIcon = 1
         CountFolder = CountFolder + 1
        End If
       Else
        Debug.Print InStr(LCase(Right(strFileName, 3)), ExtendFileName)
        If InStr(ExtendFileName, LCase(Right(strFileName, 3))) > 0 Then
         Set itmX = ListView1.ListItems.Add(, , strFileName)
         itmX.SubItems(1) = CStr(FileLen(tCurrentdir & "\" & strFileName))
         itmX.SmallIcon = 2
         itmX.SubItems(2) = FileDateTime(tCurrentdir & "\" & strFileName)
         CountFiles = CountFiles + 1
        End If
       End If
      End If
     Loop
     ListView1.Sorted = True
     ListView1.SortKey = 1
     StatusBar1.Panels(2).Text = CurrentDir
     StatusBar1.Panels(3).Text = "文件夹:" & CountFolder & " 文件:" & CountFiles
    End Sub 
      

  2.   

    strFileName = fDelInvaildChr(tFindData.cFileName)
    这里的fDelInvaildChr函数没有定义。
      

  3.   

    不行,这样:
    strFileName就为空了,
    Debug.Print InStr(LCase(Right(strFileName, 3)), ".txt")
    If InStr(".txt", LCase(Right(strFileName, 3))) > 0 Then
    就什么也不对了。
      

  4.   

    请教creazyfish(梳分头的鱼)用FSO。应该更简单
      

  5.   

    我只知道用FSO得到文件夹下的文件,但我不能实现得到文件夹下子文件夹下的文件。
      

  6.   

    我临时写了个,我测试通过了,你自己研究下好了
    Dim fso As New Scripting.FileSystemObject
    Dim sFolders As StringPrivate Sub Command1_Click()Dim attFolder() As String '存放每个子文件夹的数组sFolders = "d:\nico"GetAllSubFolder (sFolders)'将最后一个字符串去掉
    sFolders = Mid(sFolders, 1, Len(sFolders) - 1)MsgBox sFoldersattFolder = Split(sFolders, "$")For i = 0 To UBound(attFolder)
        GetFile attFolder(i)
    NextEnd Sub'取出所有子文件夹组成字符串
    Private Function GetAllSubFolder(ByVal sPath As String)
    On Error Resume Next
    Screen.MousePointer = 11Dim Folder1 As folder
    Dim subFolder As folder
    Set Folder1 = fso.GetFolder(sPath)For Each subFolder In Folder1.SubFolders
        sFolders = sFolders & "$" & subFolder.Path & "$"
    Next'用递规
    For Each subFolder In Folder1.SubFolders
        GetAllSubFolder (subFolder.Path)
    Next
    Screen.MousePointer = 0
    End Function
    Private Sub GetFile(ByVal sFolderPath As String)On Error Resume Next
        Dim Folder1 As folder
        Dim File1 As File
        Dim FileExtension As String
       
        Set Folder1 = fso.GetFolder(sFolderPath)
        For Each File1 In Folder1.Files
         Me.List1.AddItem File1.Name '取得文件名显示在listbox上
        Next
        
    End Sub