查找并列出文件名称增加以包括子文件夹

我有两个代码。 人们将search并命名目录中的每个文件夹。 另一个将列出单个文件夹中的文件和文件名。 我不够精通VBA来解决这个问题,所以我需要StackOverflow!

这里是文件名列表程序:

Sub Example1() Dim objFSO As Object Dim objFolder As Object Dim objFile As Object Dim i As Integer 'Create an instance of the FileSystemObject Set objFSO = CreateObject("Scripting.FileSystemObject") 'Get the folder object Set objFolder = objFSO.GetFolder("\\fc8fsp01\litho_recipe_amat_data") i = 1 'loops through each file in the directory and prints their names and path For Each objFile In objFolder.Files 'print file name Cells(i + 1, 1) = objFile.Name 'print file path Cells(i + 1, 2) = objFile.Path i = i + 1 Next objFile End Sub 

以下是导航子文件夹以编写文件夹名称的第二个代码:

 Option Explicit Dim i As Long, j As Long Dim searchfolders As Variant Dim FileSystemObject Sub ListOfFolders() Dim LookInTheFolder As String i = 1 LookInTheFolder = "\D: ' As you know; you should modificate this row. Set FileSystemObject = CreateObject("Scripting.FileSystemObject") For Each searchfolders In FileSystemObject.GetFolder(LookInTheFolder).SubFolders Cells(i, 1) = searchfolders i = i + 1 SearchWithin searchfolders Next searchfolders End Sub Sub SearchWithin(searchfolders) On Error GoTo exits For Each searchfolders In FileSystemObject.GetFolder(searchfolders).SubFolders j = UBound(Split(searchfolders, "\")) Cells(i, j) = searchfolders i = i + 1 SearchWithin searchfolders Next searchfolders exits: End Sub 

我需要一个代码来search所有的子文件夹,并列出所有包含的文件。 请帮助D:

由于速度问题,当我正在访问的某些文件夹出现在networking驱动器上时,我写了一个使用Windows Shell dir命令的VBA程序。 用适当的参数,这将返回基本目录中的所有文件; 以及所有的子文件夹和文件等等。 我已经把结果写到一个文本文件中,然后我读入Excel进一步处理。

与使用VBA的DIR或FSO相比,这些文件在networking驱动器上的运行速度要快五倍,而在本地计算机上则不是那么明显 – 但是我将其作为另一种方法呈现出来。

您必须设置对Windows Script Host Object Model的引用。 sDrivesBasePath用于设置起始文件夹名称。 sFileList是将结果写入文本文件的地方。

/S参数显示指定目录和所有子目录中的文件。 /B参数导致省略标题信息和摘要

如果运行CMD.EXE并在dir命令上寻求帮助,则会看到其他参数的解释。


 Public sDrive As String Public sBasePath As String Public Const sFileList As String = "C:\Users\Ron\FileList.txt" Option Explicit Sub GetDirTree() Dim WSH As WshShell Dim lErrCode As Long Set WSH = New WshShell lErrCode = WSH.Run("cmd.exe /c dir """ & sDrive & sBasePath & """/B /S >" & sFileList, 0, True) If lErrCode <> 0 Then MsgBox ("Error in GetDirTree: Error Number: " & CStr(lErrCode)) Stop End If End Sub 

这是我用来查找目录中的所有文件的function。

 Public Function RecursiveDir(colFiles As Collection, _ ByVal strFolder As String, _ strFileSpec As String, _ bIncludeSubfolders As Boolean) Dim strTemp As String Dim colFolders As New Collection Dim vFolderName As Variant 'Add files in strFolder matching strFileSpec to colFiles strFolder = TrailingSlash(strFolder) strTemp = Dir(strFolder & strFileSpec) Do While strTemp <> vbNullString colFiles.Add strFolder & strTemp strTemp = Dir Loop 'Fill colFolders with list of subdirectories of strFolder If bIncludeSubfolders Then strTemp = Dir(strFolder, vbDirectory) Do While strTemp <> vbNullString If (strTemp <> ".") And (strTemp <> "..") Then If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then colFolders.Add strTemp End If End If strTemp = Dir Loop 'Call RecursiveDir for each subfolder in colFolders For Each vFolderName In colFolders Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True) Next vFolderName End If 'Garbage collection Set colFolders = Nothing End Function 

该函数将填充给定目录中每个文件名的集合。 如果你想要,你可以设置bIncludeSubfoldersTrue ,它会recursion地search这个目录下的所有子文件夹。 要使用此function,您需要以下内容:

 Dim colFiles As New Collection ' The collection of files Dim Path As String ' The parent Directory you want to search Dim subFold As Boolean ' Search sub folders, yes or no? Dim FileExt As String ' File extension type to search for 

然后只需设置FileExt = "*.*"这将find每个文件与每个文件扩展名。 希望这有助于多一点。