VBA excel 计算文件夹(和文件)总数
VBA excel count total number of folders (and file)
我有以下脚本。想要文件夹、子文件夹和文件的数量:
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 行。
如有任何帮助/提示,我们将不胜感激!
方法如下:
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 变成了一个函数,returns 在该文件夹和子文件夹中找到的文件数量。和以前一样,这是递归的。
非递归选项:
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
我有以下脚本。想要文件夹、子文件夹和文件的数量:
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 行。
如有任何帮助/提示,我们将不胜感激!
方法如下:
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 变成了一个函数,returns 在该文件夹和子文件夹中找到的文件数量。和以前一样,这是递归的。
非递归选项:
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