跳到主要内容

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

您是否曾经尝试将某个文件夹中的所有文件名列出到工作表中,包括其子文件夹中的文件? 实际上,我们没有直接方法可以在Excel中列出文件夹及其子文件夹中的文件名,但是,今天,我将介绍一些快速的技巧来解决此问题。

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

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


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

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

2。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic应用程序 窗口。

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

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

Sub MainList()
'Updateby Extendoffice
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 运行此代码的关键,以及 弹出对话框,选择 主目录 宏名称,然后单击 运行 按钮,请参见屏幕截图:

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

5. 而在 浏览 窗口,请选择要列出所有文件名(包括子文件夹)的文件夹,请参见屏幕截图:

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

6。 指定文件夹后,然后单击 OK 按钮,并且该文件夹及其子文件夹中的所有文件名已从单元格A2列出到当前工作表中,请参见屏幕截图:

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

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

Kutools for Excel : 带有300多个便捷的Excel加载项,可以在30天内免费试用.

安装后 Kutools for Excel,请执行以下步骤:

1。 点击 企业 > 进出口 > 文件名列表,请参见屏幕截图:

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

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

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

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

备注:要列出子文件夹中的文件名,请检查 在子目录中包含文件,您还可以检查 包括隐藏的文件和文件夹 根据需要。 如果您检查 创建超链接 选项,它将为每个文件名和文件夹创建超链接。

立即下载并免费试用!

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

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

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

立即下载和免费试用Excel的Kutools!


Kutools for Excel:具有300多个方便的Excel加载项,可以在30天内免费试用,没有任何限制。 立即下载并免费试用!

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (20)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Nice work, exactly what I was trying to create. But this is 1000% better.
This comment was minimized by the moderator on the site
Sorry... I gave you the wrong code (below), here is the code I modified..
Code:
Sub MainList()
On Error Resume Next
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Dim answer As Variant
answer = False
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a Folder"
.AllowMultiSelect = False
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show = -1 Then
xDir = folder.SelectedItems(1)
Else
Exit Sub
End If
End With

Call ListFilesInFolder(xDir, True)
End Sub

Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1

' Add a space then the Folder Name to the Worksheet
rowIndex = rowIndex + 1
With Application.ActiveSheet.Cells(rowIndex, 1)
.Value = xFolder.Name
.Font.Size = 12
.Font.FontStyle = "Bold Italic"
End With
rowIndex = rowIndex + 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
rowIndex = rowIndex + 1
End If

Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing

Call MainList
End Sub


This comment was minimized by the moderator on the site
I have modified your code to make it recycle and re-run the folder dialog continuously until you press Cancel.Unfornatually it generates some errors.1. If subfolders become involved in a folder selected then the next folder selected becomes out of numerical order.2. If subfolders become involved in a folder selected the Cancel button has to be repeated and repeated depending on how many folders you have added.
Code:
Sub MainList()
'Updateby Extendoffice
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
Call ListFilesInFolder(xDir, True)
End SubSub 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

any ideas ?
This comment was minimized by the moderator on the site
Does it work on MAC too?
This comment was minimized by the moderator on the site
Hey, so if I have to just extract an extension from the whole list, where should I make changes?
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
My VBA is saying that the variable folder is not defined. Anyone know why this is?
This comment was minimized by the moderator on the site
What was the purpose of the parameter ByVal xIsSubfolders As Boolean?
This comment was minimized by the moderator on the site
Extremamente elegante este código!
This comment was minimized by the moderator on the site
Sub "GetFileOwner()" in code above is not used.
This comment was minimized by the moderator on the site
Hello, jumpjack,
The above code works well in my Excel, which Excel version do you use?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations