【问题标题】:Recursively Counting Files in Folders and Subfolders Including Root VB.NET递归计算文件夹和子文件夹中的文件,包括根 VB.NET
【发布时间】:2015-10-22 10:42:23
【问题描述】:

我正在尝试计算 CDROM 驱动器 F:\ 中文件夹和子文件夹中的文件总数 CDROM 上的文件总数为 58,但我下面的代码返回的文件总数为 39。所有 39 个文件都已计算在内包含在文件夹中,丢失的 19 个文件位于驱动器 F:\

的根目录中

如何计算根目录以及文件夹\子文件夹中的文件?

我正在使用递归搜索,因为我发现它比 IO.Directory.EnumerateFiles(DirectoryPath, "*", IO.SearchOption.AllDirectories).Count 等更适用于 Try\Catch 异常错误。

问候

乔治

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

【问题讨论】:

  • @varocabas - TotalFileCount 正在 ByRef 传递。您投反对票的答案应该可以解决 OP 的问题。
  • @varocabas 如果 Infer 开启则编译。
  • @varocarbas - 让我为我们所有人(包括我自己)在发帖时犯下草率的复制/粘贴错误表示歉意。有太多的编辑和删除,在这种情况下,我不确定我要特别道歉,但我确实道歉。就个人而言,经过 45 年的编程,我仍在寻找完美。

标签: vb.net file recursion count directory


【解决方案1】:

在您的 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 参数)并将这个值相加得到总数,那就很容易理解了。

【讨论】:

  • 请更正您的代码。现在它甚至没有编译。
  • @varocabas 如果 Infer 开启则编译。请停止删除 cmets。
  • @dbasnett(恭喜你发现了我犯的错误;但不要开始认为这很常见)。首先,这不是一个可接受的条件(= OP 应该强调这个问题)。其次:你是说写“End If”而不是“End Sub”可以通过推断来纠正?!哈哈。
  • 我们是两个不同的人,我们彼此都不认识。但是,我认为这不是在这里进行的建设性讨论。我无法看到之前编写的所有 cmets,因此我对它进行任何 cmets 都是错误的。请随时编辑或指出我的答案中的错误/改进。
  • @varocarbas - 这个答案与 Infer On 编译(可能需要 Strict Off),但它基本上向 OP 展示了他们的错误并回答了计数错误的原因。
【解决方案2】:

这是一个尝试的方法

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多