递归传输和分解文件/文件夹

本文关键字:文件夹 文件 分解 传输 递归 | 更新日期: 2023-09-27 18:32:46

我编写了一个用于处理非常大的文件共享的应用程序,当它将它们复制到新位置时,在本例中是另一个HD,它将复制的内容分解到包含4,500个或更少项目的文件夹中(要同步到在线共享点的库)。补偿长文件路径。这是我协同工作的函数的代码示例,它接收源和目标,然后执行上述复制中断:

编辑:

好的,我找到了一个解决方案,下面的这种方法正在按照我想要的方式工作,它真的很慢,你可以想象我是如何在运行时计算子项目的,所以如果有人有更好/更快的解决方案,请随时发布它。否则,非常感谢大家的帮助,非常感谢。这是我目前拥有的代码:

#region Recursive_Copy
    public static List<string> OutOfReachAreas = new List<string>();
    private static List<FileInfo> Overflow = new List<FileInfo>();
    private static List<string> AccessDeniedList = new List<string>();
    private static bool FirstTime = true;
    private static int LibraryCount { get; set; }
    private static bool ToSwith = false;
    private static int CountLimit = 4250;
    public static void RecursiveCopy(string SourceDir, string DestDir)
    {
        if (!FirstTime)
        {
            LibraryCount =
            ((Directory.GetDirectories((Program.DestinationContainer + "''" + "Library" + Program.LibraryCounter.ToString()), "*", SearchOption.AllDirectories).Count())
           + (Directory.GetFiles((Program.DestinationContainer + "''" + "Library" + Program.LibraryCounter.ToString()), "*", SearchOption.AllDirectories).Count()));
        }
        if (LibraryCount <= CountLimit && !FirstTime)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(SourceDir);
                DirectoryInfo[] dirs = dir.GetDirectories();
                FileInfo[] files = dir.GetFiles();
                string CurrentLibrary = "Library" + Program.LibraryCounter.ToString();
                if (!Directory.Exists(DestDir))
                {
                    Directory.CreateDirectory(DestDir);
                }
                if (Overflow.Count() != 0)
                {
                    foreach (var OverflowInst in Overflow)
                    {
                        string NewestLibrary = Program.DestinationContainer + "''" + "Library" + Program.LibraryCounter + "''" + OverflowInst.Name;
                        if (!File.Exists(NewestLibrary))
                        {
                            OverflowInst.CopyTo(NewestLibrary, false);
                        }
                    }
                    Overflow.Clear();
                }
                foreach (var file in files)
                {
                    try
                    {
                        DirectoryInfo TestPath = new DirectoryInfo(Program.DestinationContainer + "''" + CurrentLibrary);
                        int TestPathCount = (TestPath.GetDirectories("*", SearchOption.AllDirectories).Count()) + (TestPath.GetFiles("*", SearchOption.AllDirectories).Count());
                        if (TestPathCount <= CountLimit)
                        {
                            string temppath = Path.Combine(DestDir, file.Name);
                            DirectoryInfo TestTemp = new DirectoryInfo(temppath);
                            if (!TestTemp.Exists && !AccessDeniedList.Contains(file.Name))
                            {
                                file.CopyTo(temppath, true);
                            }
                        }
                        else
                        {
                            FileInfo OverflowToAdd = new FileInfo(file.FullName);
                            Overflow.Add(OverflowToAdd);
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        AccessDeniedList.Add(file.Name);
                    }
                }
                foreach (var subDir in dirs)
                {
                    DirectoryInfo TestPath = new DirectoryInfo(Program.DestinationContainer + "''" + CurrentLibrary);
                    int TestPathCount = (TestPath.GetDirectories("*", SearchOption.AllDirectories).Count()) + (TestPath.GetFiles("*", SearchOption.AllDirectories).Count());
                    if (TestPathCount <= CountLimit)
                    {
                        if (ToSwith)
                        {
                            ToSwith = false;
                            string PathToSwitch = Program.DestinationContainer + "''" + "Library" + Program.LibraryCounter.ToString();
                            RecursiveCopy(SourceDir, PathToSwitch);
                        }
                        string temppath = Path.Combine(DestDir, subDir.Name);
                        RecursiveCopy(subDir.FullName, temppath);
                    }
                    else
                    {
                        DirectoryInfo CurrentDir = new DirectoryInfo(DestDir);
                        RecursiveCopy(SourceDir, (Program.DestinationContainer + "''" + "Library" + Program.LibraryCounter.ToString()));
                    }
                }
            }
            catch (PathTooLongException)
            {
                DirectoryInfo DirInst = new DirectoryInfo(SourceDir);
                OutOfReachAreas.Add(DirInst.FullName);
            }
            catch (Exception e)
            {
                Console.WriteLine("'nError During Copying:'n" + e.Message);
            }
        }
        else
        {
            ++Program.LibraryCounter;
            FirstTime = false;
            ToSwith = true;
            string LibraryToMake = Program.DestinationContainer + "''" + "Library" + (Program.LibraryCounter.ToString());
            Console.WriteLine("Building Library" + (Program.LibraryCounter.ToString()) + "...");
            Directory.CreateDirectory(LibraryToMake);
            RecursiveCopy(SourceDir, LibraryToMake);
        }
    }
    #endregion

递归传输和分解文件/文件夹

就像chais在评论中说的那样,你需要子目录搜索是递归的。

MS 示例为 EACH 子目录调用自身,因此它将遍历整个目录树。您可以通过替换来执行类似操作:

    foreach (var dir in FolderList1)
    {
        Destination = Destination + "''" + dir.Name;
        CopyItems(dir, Destination);
    }

    foreach (var dir in FolderList1)
    {
        Destination = Destination + "''" + dir.Name;
        CopyItems(dir, Destination);
        foreach (Directory subDir in dir.GetDirectories())
        {
            BreakMain(subDir, Destination);
        }
    }

您还有其他一些必须修复的东西才能移动到此递归设计,但这是MS示例的要点。