【问题标题】:VBA excel count total number of folders (and file)VBA excel计算文件夹(和文件)总数
【发布时间】:2021-08-06 21:45:28
【问题描述】:

我有以下脚本。想要文件夹、子文件夹和文件的数量:

Sub CountFiles(ByVal path1 As String)

Dim fso As Object
Dim subfolder As Object
Dim file As Object
Dim folder As Object
Dim stetje As Long

Set fso = CreateObject("Scripting.FileSystemObject")

Set folder = fso.GetFolder(path1)

For Each subfolder In folder.SubFolders
 CountFiles (subfolder.path)
 
Next subfolder

For Each file In folder.Files


Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = file.path



Next file


Set fso = Nothing
Set folder = Nothing
Set subfolder = Nothing
Set file = Nothing

End Sub

你称之为:

Sub someStuff()
Call CountFiles ("c:/temp/test/")
End Sub

此脚本将所有文件夹、子文件夹和文件的路径写入 Excel 单元格

但我真正想要的是计算所有出现的总数到一个变量中。

所以不要这样:

 For Each file In folder.Files
    
    
    Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = file.path
    
    
 Next file

我想要这样的东西:

 For Each file In folder.Files
    
    number = number +  file.path.Count // of course this line is completely pseudo

 Next file

所以想要的输出例如是数字:2345 而不是 2345 行并写出路径。

任何帮助/提示将不胜感激!

【问题讨论】:

  • folder.Files.Count 是文件夹中的文件数
  • 谢谢你的回复,但我只得到这样的输出:6,3,3,6,6,3 这意味着它返回每个路径的文件数......我'想要所有文件夹和所有文件的总数。这只是每个循环的最终文件。因此,即使我将它们加在一起,我也只能得到每个路径末尾的文件数。所以改为

标签: excel vba foreach vba7


【解决方案1】:

这是一种方法:

Function CountFiles(ByVal path As String) As Long

    Dim fso As Object
    Dim folder As Object
    Dim subfolder As Object
    Dim amount As Long
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set folder = fso.GetFolder(path)
    For Each subfolder In folder.SubFolders
        amount = amount + CountFiles(subfolder.path)
    Next subfolder
    
    amount = amount + folder.Files.Count
    
    Set fso = Nothing
    Set folder = Nothing
    Set subfolder = Nothing
    
    CountFiles = amount

End Function

Sub someStuff()
    MsgBox CountFiles("c:/temp/test/")
End Sub

我已经把 sub 变成了一个函数,它返回在那个文件夹和子文件夹中找到的文件的数量。和以前一样,这是递归的。

【讨论】:

    【解决方案2】:

    非递归选项:

    Function FilesCount(fldr As String)
        Dim colFolders As New Collection, fso, num As Long, f As Object, sf As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        colFolders.Add fso.getfolder(fldr) 'add the starting folder
        
        num = 0
        Do While colFolders.Count > 0      'while we still have folders to process...
            Set f = colFolders(1)          '   get the first folder from the collection
            colFolders.Remove 1            '   and remove it from the collection
            num = num + f.Files.Count      '   Add # of files in that folder
            For Each sf In f.subfolders    '     and add each subfolder into the collection
                colFolders.Add sf
            Next sf
        Loop
        FilesCount = num
    End Function
    

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 2017-07-07
      • 2020-07-18
      • 1970-01-01
      • 2012-06-03
      • 2017-05-07
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      相关资源
      最近更新 更多