微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

需要合并 2 个文件夹,每个文件夹都有数十万个文件 VB.net

如何解决需要合并 2 个文件夹,每个文件夹都有数十万个文件 VB.net

我需要合并这些在每个目录中都有大量文件文件夹。我会使用 folder.copy 但这需要永远。

我大部分时间都在使用 xcopy 进程来处理这个问题,但是我从 xcopy 收到错误,说没有重复时有重复。因此,我试图找到一个与 xcopy 一样快甚至更快的可靠工作。

我之前尝试过的是:

Private Sub MergeF(ByVal TargetFolder As String,ByVal MergeeFolder As String)
    For Each F As String In IO.Directory.GetFiles(MergeeFolder)
        If IO.File.Exists(IO.Path.Combine(TargetFolder,IO.Path.GetFileName(F))) Then
            Dim FileA As New IO.FileInfo(IO.Path.Combine(
                MergeeFolder,IO.Path.GetFileName(F)))
            Dim FileB As New IO.FileInfo(IO.Path.Combine(
                TargetFolder,IO.Path.GetFileName(F)))
            If FileA.Length <> FileB.Length Then
                Dim index As Integer = 1
                Do
                    Dim NewFileName = IO.Path.Combine(TargetFolder,IO.Path.GetFileName(F.Insert(F.Length - 4,CStr(index))))
                    If IO.File.Exists(NewFileName) Then
                        index += 1
                    Else
                        IO.File.copy(F,NewFileName)
                        IO.File.Delete(F)
                        Exit Do
                    End If
                Loop
            End If
        Else
            IO.File.Move(IO.Path.Combine(MergeeFolder,IO.Path.GetFileName(F)),IO.Path.Combine(TargetFolder,IO.Path.GetFileName(F)))
        End If
    Next
End Sub

解决方法

https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories

异步也应该加快速度;

https://docs.microsoft.com/en-us/dotnet/standard/io/asynchronous-file-i-o

如果这还不够快,那么我认为您的下一个选择是尝试直接使用 win32api

将代码转换为可以使用的vb;

https://codeconverter.icsharpcode.net/ 要么 https://converter.telerik.com/

  • 祝你好运!
,

您的实现问题在于您在循环内调用 File.Exists。这会一次又一次地扫描目标目录中的文件并使其变慢。更好的方法是将目标目录中文件的文件名加载到 HashSet(Of T) 中。此集合中的查找速度非常快 - 比 File.Exists 快得多。

Private Sub MergeF(ByVal TargetFolder As String,ByVal MergeeFolder As String)
    Dim targetFiles = New HashSet(Of String)(
        IO.Directory.GetFiles(TargetFolder) _
            .Select(Function(f) IO.Path.GetFileName(f)),StringComparer.OrdinalIgnoreCase)

    For Each sourcePath As String In IO.Directory.GetFiles(MergeeFolder)
        Dim file As String = IO.Path.GetFileName(sourcePath)
        Dim targetPath As String = IO.Path.Combine(TargetFolder,file)

        If targetFiles.Contains(file) Then
            Dim sourceInfo As New IO.FileInfo(sourcePath)
            Dim targetInfo As New IO.FileInfo(targetPath)
            If sourceInfo.Length <> targetInfo.Length Then
                Dim index As Integer = 1
                Do
                    Dim fileWithoutExt =
                    Path.GetFileNameWithoutExtension(file)
                    Dim extension = Path.GetExtension(file)
                    file = fileWithoutExt & index & extension
                    If targetFiles.Contains(file) Then
                        index += 1
                    Else
                        targetPath = IO.Path.Combine(TargetFolder,file)
                        targetFiles.Add(file) 'Upate the HashSet
                        IO.File.Move(sourcePath,targetPath)
                        Exit Do
                    End If
                Loop
            End If
        Else
            targetFiles.Add(file) 'Upate the HashSet
            IO.File.Move(sourcePath,targetPath)
        End If
    Next
End Sub

请注意,我还通过使用适当的变量减少了 Path.CombinePath.GetFileName 的数量。我调用了没有目录 ...File 和完整路径 ...Path 的文件名。

我使用 HashSet 初始化 StringComparer.OrdinalIgnoreCase,使其忽略文件名的字符大小写,因为 Windows 文件系统也会忽略大小写。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。