Excel VBA:搜索文件夹和子目录以查找不包含某些子目录的文件

时间:2017-07-14 15:42:32

标签: excel vba excel-vba

我找到了一些在线搜索目录及其子目录以查找满足搜索条件的文件的代码。

我想将此代码编辑为:

  1. 找到第一个匹配的文件后停止
  2. 使用"历史记录"忽略所有子目录用它的名字('历史','历史'等)
  3. 创建目录结构的人在文件名中使用了空格,因此要忽略的文件夹示例包括"工具历史记录","工具历史记录"中的所有子目录/ p>

    我找到的代码如下(抱歉没有引用来源,我无法记住我发现它的位置)

    Function RecursiveDir(colFiles As Collection, _
                                 strFolder As String, _
                                 strFileSpec As String, _
                                 bIncludeSubfolders As Boolean)
        ' Search a folder and each of its subfolders for any files that meet the citerion given in
        ' strFileSpec
    
        ' colFiles - the name of the collection to add the output to
        ' strFolder - The path to the parent directory
        ' strFileSpec - The condition of the filename being searched for (for example all pdf files)
        ' bIncludeSubfolders - Boolean, include all subfolders in the search
    
        ' THIS FUNCTION IS SUBOPTIMAL AND VERY SLOW, PLEASE REVISIT IF USED REGULARLY
    
        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
    
        If bIncludeSubfolders Then
            'Fill colFolders with list of subdirectories of strFolder
            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
    
    End Function
    
    Function TrailingSlash(strFolder As String) As String
        ' Search for and remove a trailing slash in the directory pathname
        If Len(strFolder) > 0 Then
            If Right(strFolder, 1) = "\" Then
                TrailingSlash = strFolder
            Else
                TrailingSlash = strFolder & "\"
            End If
        End If
    End Function
    

    这段代码很慢,所以如果有人有更快的话,我会非常感激。

    非常感谢

1 个答案:

答案 0 :(得分:0)

如果我是你,我会这样做。

# create a data frame with the number of rows equal to the number of
# index iteration (in this case 3). The variable must be of the same type
# of your iteration output.
df <- data.frame(x = 1:3, y = rep("some", 3), stringsAsFactors = FALSE)
some_text <- c("dog", "cat", "duck")

for (i in 1:3) {
  x <-  i + 10
  y <-  some_text[i]
  iteration_output <- data.frame(x, y, stringsAsFactors = FALSE)
  # replace each variable of the given index row from the iteration output
  df[i, "x"] <- iteration_output$x
  df[i, "y"] <- iteration_output$y
}

df