提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何将文件夹和子文件夹中的所有文件列入工作表?

你有没有试过将文件夹中的所有文件名列入工作表,包括其子文件夹中的文件? 事实上,我们没有直接的方式来列出文件夹及其子文件夹在Excel中的文件名,但是,今天,我将介绍一些快速的技巧来解决这个工作。

用VBA代码列出文件夹和子文件夹中的所有文件名

使用Kutools for Excel快速轻松地列出文件夹和子文件夹中的所有文件名


将包含子文件夹的文件夹中的所有文件列入工作表中:

Kutools for Excel's 文件名列表 功能,您可以快速列出包含子文件夹的文件夹中的所有文件名到新的工作表中。 现在点击下载并免费试用Kutools for Excel!

doc文件夹子文件夹9中的文件列表文件

Kutools for Excel:比200方便的Excel加载项,可以在60天免费试用。 下载并免费试用现在!



通常,Excel没有内置功能来处理此任务,但是,您可以应用以下VBA代码来完成此问题。

1。 激活一个将列出文件名的新工作表。

2。 按住 ALT + F11 在Excel中的键,它打开 Microsoft Visual Basic for Applications 窗口。

3。 点击 插页 > 模块,并将以下代码粘贴到 模块窗口.

VBA代码:列出文件夹和子文件夹中的所有文件名

Sub MainList()
'Updateby20150706
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
Call ListFilesInFolder(xDir, True)
End Sub
Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1
For Each xFile In xFolder.Files
  Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
  rowIndex = rowIndex + 1
Next xFile
If xIsSubfolders Then
  For Each xSubFolder In xFolder.SubFolders
    ListFilesInFolder xSubFolder.Path, True
  Next xSubFolder
End If
Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing
End Sub
Function GetFileOwner(ByVal xPath As String, ByVal xName As String)
Dim xFolder As Object
Dim xFolderItem As Object
Dim xShell As Object
xName = StrConv(xName, vbUnicode)
xPath = StrConv(xPath, vbUnicode)
Set xShell = CreateObject("Shell.Application")
Set xFolder = xShell.Namespace(StrConv(xPath, vbFromUnicode))
If Not xFolder Is Nothing Then
  Set xFolderItem = xFolder.ParseName(StrConv(xName, vbFromUnicode))
End If
If Not xFolderItem Is Nothing Then
  GetFileOwner = xFolder.GetDetailsOf(xFolderItem, 8)
Else
  GetFileOwner = ""
End If
Set xShell = Nothing
Set xFolder = Nothing
Set xFolderItem = Nothing
End Function

4。 将代码粘贴到模块后,按 F5 运行这个代码的关键,和一个 弹出对话框,选择 MainList 宏名称,然后单击 按钮,看截图:

doc文件夹子文件夹1中的文件列表文件

5。 和在 浏览 窗口中,请选择要列出包括子文件夹在内的所有文件名的文件夹,请参阅屏幕截图:

doc文件夹子文件夹2中的文件列表文件

6。 指定文件夹后,点击 OK 按钮,并且文件夹及其子文件夹中的所有文件名都已列入单元格A2的当前工作表中,请参阅截图:

doc文件夹子文件夹3中的文件列表文件
 1
doc文件夹子文件夹4中的文件列表文件

使用上面的代码,你可以列出文件名,有时候,你需要列出其他属性,如文件大小,文件类型,创建时间,包含文件夹等。 Kutools for Excel 包含一个有用的功能 - 文件名列表,通过此功能,您可以快速列出文件夹及其子文件夹中的所有或特定类型的文件名。

Kutools for Excel : 与超过300方便的Excel加载项,在60天免费试用没有限制.

安装后 Kutools for Excel请按以下步骤操作:

1。 点击 企业服务 > 导入/导出 > 文件名列表,看截图:

doc文件夹子文件夹5 5中的文件列表文件

doc文件夹子文件夹6 6中的文件列表文件

2。 在 文件名列表 对话框,请执行以下操作:

A:点击 doc文件夹子文件夹7中的文件列表文件按钮选择要列出文件名的文件夹;

B:指定要从中列出的文件类型 文件类型 部分;

C:从中选择一个您想要显示的文件大小单位 文件大小单位 部分。

注意:要列出子文件夹中的文件名,请检查 将文件包含在子目录中,如果你检查 创建超链接 选项,它会为每个文件名和文件夹创建超链接。

立即下载并免费试用!

3。 然后点击 OK 按钮时,所选文件夹及其子文件夹中包含的所有文件都将在新工作表中显示以下属性。 看截图:

doc文件夹子文件夹8中的文件列表文件

点击了解有关此文件名列表实用程序的更多详细信息。

立即下载并免费试用Kutools for Excel!


Kutools for Excel:比200方便的Excel加载项,可以在60天免费试用。 下载并免费试用现在!


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    JDay · 2 months ago
    Really new to VBA. how do i use the above code but have the file path built into it so i don't have to search for it every time?
  • To post as a guest, your comment is unpublished.
    James · 6 months ago
    My VBA is saying that the variable folder is not defined. Anyone know why this is?
  • To post as a guest, your comment is unpublished.
    Chris K · 11 months ago
    What was the purpose of the parameter ByVal xIsSubfolders As Boolean?
  • To post as a guest, your comment is unpublished.
    Leandro Barbosa · 11 months ago
    Extremamente elegante este código!
  • To post as a guest, your comment is unpublished.
    jumpjack · 1 years ago
    Sub "GetFileOwner()" in code above is not used.
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, jumpjack,
      The above code works well in my Excel, which Excel version do you use?
  • To post as a guest, your comment is unpublished.
    Brett · 1 years ago
    Thankyou for this code it has helped me with a request a work, I am now able to import these results into Access for further transformation.Appreciate it.
  • To post as a guest, your comment is unpublished.
    David · 1 years ago
    This helped me to convert my code to get the recursive call, but I wonder about the way you use objects. I prefer to define my objects as what they are, be they workbooks, worksheets or file system objects. (dim wbDest as excel.workbook, dim wsDest as excel.worksheet for example. This way I never have to worry about what the active sheet is.

    I also wondered about the use of .show - if.show <>-1, and can't help feeling that it would be easier for people to understand if instead you used if.show = false.

    And in terms of writing to the destination, I felt that using cells and formula seemed to be a long way round

    So in my code I have, for example,
    wsDest.Range("B" & CurRow) = fil.name
    (CurRow = Current Row)

    I'm not criticising, just wondering if you do it like this for any reason other than personal preference. - Always willing to learn


    But apart from that thanks for the help
  • To post as a guest, your comment is unpublished.
    Ana · 1 years ago
    Thank you so much for the useful article!
    How can I automatically update the list and spot the changes?
  • To post as a guest, your comment is unpublished.
    Ankit · 1 years ago
    Not sure.. but does it consider long folders. i wrote a similar code and it fails where the folder/file path exceeds 256 characters ? Thoughts please ?
  • To post as a guest, your comment is unpublished.
    Nir · 1 years ago
    great!!! love it, thanks
  • To post as a guest, your comment is unpublished.
    SATISH · 1 years ago
    HOWTO MAKE THIS QUERY WORK TO GET THE FILENAMES LISTED FROM B2 cell instead of A2 ?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hi, Satish,
      The following code can help you to put the filenames to any cell you selected, please try it:

      Sub MainList()
      On Error Resume Next
      Set xRg = Application.InputBox("Please select a cell to put the filenames:", "KuTools For Excel", Selection.Address, , , , , 8)
      If xRg Is Nothing Then Exit Sub
      Set xRg = xRg(1)
      Set Folder = Application.FileDialog(msoFileDialogFolderPicker)
      If Folder.Show <> -1 Then Exit Sub
      xDir = Folder.SelectedItems(1)
      Call ListFilesInFolder(xRg, xDir, True)
      End Sub
      Sub ListFilesInFolder(ByVal xRg As Range, ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
      Dim xFileSystemObject As Object
      Dim xFolder As Object
      Dim xSubFolder As Object
      Dim xFile As Object
      Dim rowIndex As Long
      Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
      Set xFolder = xFileSystemObject.GetFolder(xFolderName)
      rowIndex = 1
      For Each xFile In xFolder.Files
      xRg.Formula = xFile.Name
      Set xRg = xRg.Offset(rowIndex)
      rowIndex = 1
      Next xFile
      If xIsSubfolders Then
      For Each xSubFolder In xFolder.SubFolders
      ListFilesInFolder xRg, xSubFolder.Path, True
      Next xSubFolder
      End If
      Set xFile = Nothing
      Set xFolder = Nothing
      Set xFileSystemObject = Nothing
      End Sub
  • To post as a guest, your comment is unpublished.
    Anilkumar · 2 years ago
    dear,

    Thanks for this wonderful utility.....!!!


    anilkumar
  • To post as a guest, your comment is unpublished.
    Adrian Badea · 3 years ago
    Good article

    Thanks :D