子文件夹遍历

发布于 2024-09-19 12:27:09 字数 223 浏览 2 评论 0原文

我在 c:\MyData 中有一组 3 层以上的子文件夹,其中包含超过 20k 个文件。

我的 E 驱动器上有一组几乎相同的子文件夹,位于 e:\projects\massdata

我想签入 C 以及 E 中已存在的任何内容(相同的文件夹名称、相同的文件名、相同的大小) ),我想从 C 中删除。

遍历此文件夹结构的最佳方法是什么?

I have a set of subfolders 3 levels deep over with over 20k files in c:\MyData.

There is an almost identical set of subfolders on my E drive at e:\projects\massdata

I want to check in C and anything that already exists in E (same folder name, same file name, same size), I want to delete from C.

What is my best way of traversing this folder structure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2024-09-26 12:27:09

使用连接运算符怎么样? 像更新后第二次编辑一样加入文件名

public void cleanUp()
    {
        var cFiles = Directory.GetFiles(@"c:\MyData","*.*",SearchOption.AllDirectories);
        var fFiles = Directory.GetFiles(@"e:\projects\massdata","*.*",SearchOption.AllDirectories);
        Func<string, string, Tuple<string, long>> keySelector = (path, root) =>
            new Tuple<string, long>(path.Replace(root, ""), new FileInfo(path).Length);

        foreach (var file in cFiles.Join(fFiles, f => keySelector(f,@"e:\projects\massdata"), c => keySelector(c,@"c:\MyData"), (c, f) => c))
        {
            File.Delete(file);
        }
    }


键选择器现在应该满足您的要求。如果我误解了他们的话。这肯定相当容易,所以看看你需要改变什么。如果没有请发表评论:)

how about using the join operator. joining on filename like this

public void cleanUp()
    {
        var cFiles = Directory.GetFiles(@"c:\MyData","*.*",SearchOption.AllDirectories);
        var fFiles = Directory.GetFiles(@"e:\projects\massdata","*.*",SearchOption.AllDirectories);
        Func<string, string, Tuple<string, long>> keySelector = (path, root) =>
            new Tuple<string, long>(path.Replace(root, ""), new FileInfo(path).Length);

        foreach (var file in cFiles.Join(fFiles, f => keySelector(f,@"e:\projects\massdata"), c => keySelector(c,@"c:\MyData"), (c, f) => c))
        {
            File.Delete(file);
        }
    }

Second Edit after update:
The key selector should now meet your requirement. If I've misunderstood them. It sure be rather easy so see what you need to change. If not drop a comment :)

眼波传意 2024-09-26 12:27:09

递归地遍历每个目录中的所有文件。

hashMap中创建一个字符串,描述E中文件的相对路径、文件大小等。然后,在通过 C 时检查特定文件相对路径是否存在,如果存在则将其删除。

例如,该字符串可以是 [FILENAME]##[FILESIZE]##[LASTEDITER]。

这是在 C# 中递归搜索的一种方法:
http://support.microsoft.com/kb/303974

Recursively go thru all files in each directory.

Create a string describing the relative path,file size, etc. of the files in E in a hashMap. Then just check if a specific files relative path exists, when going thru C, and delete it if so.

The string could for instance be [FILENAME]##[FILESIZE]##[LASTEDITER].

Here is one way to search recursively in C#:
http://support.microsoft.com/kb/303974

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文