如何读取在指定目录中的所有文件夹及文件名?

解决方案 »

  1.   

    这么简单的事百度一下,锻炼独立解决问题的能力,不比给分等别人回答强么?
    给个提示:
    方法一:filelist控件
    方法二:借助DIR递归搜索
      

  2.   

    '先引用microsoft scripting Runtime
    Private Sub Command1_Click()
    Dim fso As New FileSystemObject
        Set objFolder = fso.GetFolder("E:\media")
        For Each Path In objFolder.SubFolders
            Debug.Print Path
            For Each Files In fso.GetFolder(Path).Files
                Debug.Print Files
            Next
        Next
    End Sub
      

  3.   

    加强说明下1L的回答方法一:filelist控件
    添加drivelistbox、dirlistbox、filelistbox控件各一个并输入以下代码Option ExplicitPrivate Sub Dir1_Change()
    File1.Path = Dir1.Path
    End SubPrivate Sub Drive1_Change()
    Dir1.Path = Drive1.Drive
    End Sub
    方法二:借助DIR递归搜索
    添加一个textbox、一个commandbutton(这里我用的容器是textbox,你也可以用别的)输入以下代码:Option Explicit
    Private Sub Command1_Click()
    Dim a As String
    a = Dir("C:\", vbDirectory Or vbHidden Or vbSystem)
    Text1.Text = Text1.Text & "、" & a
    Do Until a = ""
    a = Dir
    Text1.Text = Text1.Text & "、" & a
    Loop
    End Sub帮你这一次啊,记准喽,下次不许再来提这种度娘都知道的问题
      

  4.   

    返回D盘所有的文件及文件夹名称到立即窗口
    Sub tt()
    f_name = Dir("d:\", 23)
    Do While f_name <> ""
    Debug.Print f_name
    f_name = Dir
    Loop
    End Sub
      

  5.   

    返回D盘所有的文件及文件夹名称到立即窗口
    Sub tt()
    f_name = Dir("d:\", 23)
    Do While f_name <> ""
    Debug.Print f_name
    f_name = Dir
    Loop
    End Sub
      

  6.   

    FSO 遍历文件夹和文件http://blog.csdn.net/z_wenqian/article/details/6368870
      

  7.   

    网上的代码Public Function TreeSearch(ByVal sPath As String, ByVal sFileSpec As String, sFiles() As String) As Long
        Dim n As Long
        Static lngFiles As Long '文件数目
        Dim sDir As String
        Dim sSubDirs() As String '存放子目录名称
        Dim lngIndex As Long
        Dim lngTemp&
        If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
        sDir = Dir(sPath & sFileSpec)
       '获得当前目录下文件名和数目
        Do While Len(sDir)
          lngFiles = lngFiles + 1
          ReDim Preserve sFiles(1 To lngFiles)
          sFiles(lngFiles) = sPath & sDir
          sDir = Dir
        Loop
       '获得当前目录下的子目录名称
        lngIndex = 0
        sDir = Dir(sPath & "*.*", vbDirectory)
        Do While Len(sDir)
          If Left(sDir, 1) <> "." And Left(sDir, 1) <> ".." Then '' 跳过当前的目录及上层目录
         '找出子目录名
            If GetAttr(sPath & sDir) And vbDirectory Then
              lngIndex = lngIndex + 1
             '保存子目录名
              ReDim Preserve sSubDirs(1 To lngIndex)
              sSubDirs(lngIndex) = sPath & sDir & "\"
            End If
          End If
          sDir = Dir
        Loop    For lngTemp = 1 To lngIndex
          '查找每一个子目录下文件,这里利用了递归
          Call TreeSearch(sSubDirs(lngTemp), sFileSpec, sFiles())
        Next lngTemp
        TreeSearch = lngFiles
      End Function
      

  8.   

    遍历一个文件夹下的所有文件和子文件夹Option ExplicitPublic Sub 遍历文件夹和文件(sFolder As String)
        Dim fs As Object
        On Error Resume Next
        Set fs = CreateObject("Scripting.FileSystemObject")
        File_Folder_List (fs.GetFolder(sFolder))
        Set fs = Nothing
    End SubPrivate Sub File_Folder_List(df As Object)'循环处理文件集合    Dim objFile As Object, objSubFolder As Object    '文件集合
        For Each objFile In df.Files
            '
            '
            '文件处理过程
            '
            '
        Next objFile    Set objFile = Nothing    '文件夹集合
        For Each objSubFolder In df.SubFolders
            
            '
            '
            '文件夹处理过程
            '
            '
            
            File_Folder_List objSubFolder   '递归循环处理文件夹
            
        Next objSubFolder    Set objSubFolder = Nothing
    End Sub