【发布时间】: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 这意味着它返回每个路径的文件数......我'想要所有文件夹和所有文件的总数。这只是每个循环的最终文件。因此,即使我将它们加在一起,我也只能得到每个路径末尾的文件数。所以改为