递归计算文件夹和子文件夹(包括根VB.NET)中的文件数
我正在尝试计算CDROM驱动器F:\CDROM上的文件夹和子文件夹中的文件总数是58,但下面代码返回的文件总数是39。统计的所有39个文件都包含在文件夹中,丢失的19个文件位于驱动器F的根目录中:\ 如何计算根目录以及文件夹\子文件夹中的文件数 我正在使用递归搜索,因为我发现它在处理Try\Catch异常错误时比IO.Directory.EnumerateFiles(DirectoryPath,“*”,IO.SearchOption.AllDirectories).Count等更好 问候 乔治递归计算文件夹和子文件夹(包括根VB.NET)中的文件数,vb.net,file,recursion,count,directory,Vb.net,File,Recursion,Count,Directory,我正在尝试计算CDROM驱动器F:\CDROM上的文件夹和子文件夹中的文件总数是58,但下面代码返回的文件总数是39。统计的所有39个文件都包含在文件夹中,丢失的19个文件位于驱动器F的根目录中:\ 如何计算根目录以及文件夹\子文件夹中的文件数 我正在使用递归搜索,因为我发现它在处理Try\Catch异常错误时比IO.Directory.EnumerateFiles(DirectoryPath,“*”,IO.SearchOption.AllDirectories).Count等更好 问候 乔治
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
在Search2方法中,您应该在foreach目录循环之外找到文件。您的方法正在查找这些文件夹中的子文件夹和文件,而不是当前文件夹中的文件 试试这个
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
此外,我认为,如果您只返回文件数(使用函数而不是sub和ByRef参数)并将该值相加以获得总数,那么就很容易理解了。下面是一个可以尝试的方法
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
测试
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
@varucableas-TotalFileCount正在由ref传递。您否决的答案应该可以解决OP的问题。@varocabas,如果启用了“推断”,它将编译。@varocabas-让我为我们所有人道歉,包括我在内,他们在发布时犯了草率的复制/粘贴错误。有这么多的编辑和删除,在这种情况下,我不知道我特别道歉,但我道歉。就个人而言,经过45年的编程,我仍然在寻找完美。请更正您的代码。现在它甚至还没有编译。@如果启用了“推断”,它将编译。请停止删除评论。@dbasnett(祝贺您发现了我犯的错误;但不要开始认为这是正常的)。首先,这不是一个可接受的条件(=OP应该突出显示此问题)。第二:你是说写“结束如果”而不是“结束子”可以通过推断更正?!哈哈,我们是两个不同的人,我们彼此都不认识。然而,我认为这不是一场建设性的讨论。我无法看到以前写的所有评论,所以我对它发表任何评论都是错误的。请随意编辑或指出我答案中的错误/增强。@varocarbas-此答案编译时使用了“推断开启”(可能需要严格关闭),但它基本上向OP显示了他们的错误,并回答了计数错误的原因。