递归计数包含Root VB.NET的文件夹和子文件夹中的文件

时间:2015-10-22 10:42:23

标签: 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

2 个答案:

答案 0 :(得分:2)

在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参数),我会很容易理解,并将此值相加以得到总数。

答案 1 :(得分:0)

这是一种尝试

的方法
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