[英]Recursively Counting Files in Folders and Subfolders Including Root VB.NET
I am trying to count the total number of files in folders and subfolders in CDROM drive F:\\ The total number of files on the CDROM is 58 but the total file count returned by my code below is 39. All 39 files counted are contained in folders and the missing 19 files are in the root of drive F:\\ 我正在尝试计算CDROM驱动器F:\\的文件夹和子文件夹中的文件总数,CDROM上的文件总数为58,但下面的代码返回的文件总数为39。所有计数的39个文件都包含在文件夹和缺少的19个文件位于驱动器F:\\的根目录中
How can I count the files in the root as well as the folders \\ subfolders? 如何计算根目录中的文件以及文件夹\\子文件夹?
I am using a recursive search as I found it works better with Try\\Catch exception errors than IO.Directory.EnumerateFiles(DirectoryPath, "*", IO.SearchOption.AllDirectories).Count etc. 我正在使用递归搜索,因为我发现它比Try.Catch异常错误更有效,而不是IO.Directory.EnumerateFiles(DirectoryPath,“ *”,IO.SearchOption.AllDirectories).Count等。
Regards 问候
Georgy 乔治
Public Sub Search1()
Dim DirectoryPath = TextBox1.Text
Dim TotalFileCount As Long = 0
Search2(DirectoryPath, TotalFileCount)
MsgBox(TotalFileCount)
End Sub
Public Sub Search2(ByVal DirectoryPath As String, ByRef TotalFileCount As Integer)
Dim FileName As String = Nothing
Try
For Each Directory In IO.Directory.GetDirectories(DirectoryPath)
For Each File In IO.Directory.GetFiles(Directory, "*")
FileName = New IO.FileInfo(File).FullName
TotalFileCount += 1
Next
Search2(Directory, TotalFileCount)
Next
Catch ua As UnauthorizedAccessException
ListBox1.Items.Add("Exception: " & ua.Message & " " & FileName)
Catch ex As Exception
ListBox1.Items.Add("Exception: " & ex.Message & " " & FileName)
End Try
End Sub
In your Search2 method you should find files outside the foreach loop of directories. 在Search2方法中,您应该在目录的foreach循环之外找到文件。 Your method is looking for subfolders and files inside these folders but not files inside the current folder.
您的方法是在这些文件夹中查找子文件夹和文件,而不在当前文件夹中查找文件。
Try this 尝试这个
Public Sub Search2(ByVal DirectoryPath As String, ByRef TotalFileCount As Integer)
Dim FileName As String = Nothing
Try
For Each Directory In IO.Directory.GetDirectories(DirectoryPath)
Search2(Directory, TotalFileCount)
Next
For Each File In IO.Directory.GetFiles(DirectoryPath, "*")
FileName = New IO.FileInfo(File).FullName
TotalFileCount += 1
Next
Catch ua As UnauthorizedAccessException
ListBox1.Items.Add("Exception: " & ua.Message & " " & FileName)
Catch ex As Exception
ListBox1.Items.Add("Exception: " & ex.Message & " " & FileName)
End Try
End Sub
In addition It would be easy to understand, I think, if you just return the number of files (use function instead of sub and a ByRef parameter) and sum this value to get the total number. 另外,我认为,如果您仅返回文件数(使用函数而不是sub和ByRef参数)并求和该值以得出总数,那将很容易理解。
Here is a method to try 这是一种尝试的方法
Public Function FileCount(DirPath As String, Optional IncludeSubDirs As Boolean = True) As Long
Dim rv As Long = 0L
Try
If IncludeSubDirs Then
For Each dirname As String In IO.Directory.GetDirectories(DirPath)
rv += FileCount(dirname, IncludeSubDirs)
Next
rv += IO.Directory.GetFiles(DirPath).Length
End If
Catch ua As UnauthorizedAccessException
'Stop
Catch ex As Exception
'Stop
End Try
Return rv
End Function
Tested with 经过测试
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim DirectoryPath As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Dim stpw As Stopwatch = Stopwatch.StartNew
Dim TotalFileCount As Long = FileCount(DirectoryPath)
stpw.Stop()
Dim rslts As String = String.Format("Total Files = {0:n0}. Took {1:n0} ms.", TotalFileCount, stpw.ElapsedMilliseconds)
Debug.WriteLine(rslts)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.