遍历包含子文件夹的文件夹以查找Excel文件

时间:2016-10-12 09:04:03

标签: vba excel-vba excel

我想遍历包含多个子文件夹的文件夹,以查找包含指定名称的excel文件,并对找到的excel文件执行操作。 有什么建议我能做到吗?

我找到了类似的东西,但它不起作用:

Application.ScreenUpdating = False
 Application.DisplayAlerts = False

 With Application.FileSearch
 .NewSearch
 .LookIn = "C:\temp 1" ' your drive / directory here
 .SearchSubFolders = True
 .FileName = ".xls" ' all files ending in xls
 .FileType = msoFileTypeExcelWorkbooks
 If .Execute() > 0 Then
 ' how many files are there in the selected folder?
 MsgBox "There were " & .FoundFiles.Count & " file(s) found."

 For i = 1 To .FoundFiles.Count
 Workbooks.Open .FoundFiles(i), 0
 '
 ' code 
 '

 '
 ActiveWorkbook.Save
 ActiveWorkbook.Close
 Next i
 Else
 MsgBox "There were no files found."
 End If
 End With


 Application.ScreenUpdating = True
 Application.DisplayAlerts = True

 MsgBox "All Done!"

3 个答案:

答案 0 :(得分:1)

因此,对于指定目录中的文件:

https://msdn.microsoft.com/en-us/library/dk008ty4(v=vs.90).aspx

Dim MyFile, MyPath, MyName As String 
' Returns "WIN.INI" if it exists.
MyFile = Dir("C:\WINDOWS\WIN.INI")   

' Returns filename with specified extension. If more than one *.INI 
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")

' Call Dir again without arguments to return the next *.INI file in the 
' same directory.
MyFile = Dir()

' Return first *.TXT file, including files with a set hidden attribute.
MyFile = Dir("*.TXT", vbHidden)

' Display the names in C:\ that represent directories.
MyPath = "c:\"   ' Set the path.
MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
Do While MyName <> ""   ' Start the loop.
      ' Use bitwise comparison to make sure MyName is a directory. 
      If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then 
         ' Display entry only if it's a directory.
         MsgBox(MyName)
      End If   
   MyName = Dir()   ' Get next entry.
Loop

或者对于所有子文件夹,请看一下:

Loop Through All Subfolders Using VBA

Dim FileSystem As Object
Dim HostFolder As String

HostFolder = "C:\"

Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)

Sub DoFolder(Folder)
    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next
    Dim File
    For Each File In Folder.Files
        ' Operate on each file
    Next
End Sub

答案 1 :(得分:1)

我知道的最好方法是使用批次:

dir /b "D:\mondossier\" >"C:\tempfolder\TEMP.txt"

然后将其作为文字阅读:

CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\tempfolder\TEMP.txt")

比juste FSO更快,除非错误

答案 2 :(得分:-1)

void DirSearch(string sDir) 
{
    try 
    {
       foreach (string d in Directory.GetDirectories(sDir)) 
       {
        foreach (string f in Directory.GetFiles(d, txtFile.Text)) 
        {
           if(f.EndsWith("MySearchPattern.xlsx"))
           lstFilesFound.Items.Add(f);
        }
        DirSearch(d);
       }
    }
    catch (System.Exception excpt) 
    {
        Console.WriteLine(excpt.Message);
    }
}

请参阅:de:https://support.microsoft.com/de-de/kb/303974

en:https://support.microsoft.com/en-us/kb/303974