C#如何递归列出一个目录下的所有文件并将所有文件复制到另一个目录

发布于 2024-11-09 04:35:15 字数 507 浏览 0 评论 0原文

我正在尝试编写可递归读取某个文件夹的 ac# 控制台应用程序。 这些文件夹中有数千张 .jpg 图像。

文件夹结构在某些级别上非常深,示例如下所示:

Scc-LocalPhoto/testfiles/1997/JAN-JUN 1997/1997年4月/7.4.97 - 11.4.97/FRI11.4.97/

正如你所看到的,文件夹结构相当混乱,但我无法控制它。

我的任务是通读所有文件夹。从图像中提取元数据并存储在 XML 文件中。然后,我需要复制同一布局中的所有文件夹并将它们粘贴到新文件夹中。

我想我将能够读取所有目录并从图像中提取元数据并将其保存到 xml 文件中。

我不知道该怎么做是复制并粘贴所有文件夹和图像,并将它们粘贴到保持相同文件夹结构的新目录中。

有谁知道执行此任务的有效方法,或者是否有任何可用的项目、代码我可以用作起点。

我对 C# 和编写控制台应用程序相当陌生。 感谢您抽出时间。

I am trying to write a c# console application that recursively reads through a certain folder.
In these folders are thousands of .jpg images

The folder structure is very deep in some levels and an example look like this:

Scc-LocalPhoto/testfiles/1997/JAN-JUN
1997/APRIL 1997/7.4.97 -
11.4.97/FRI11.4.97/

As you can see the folder structure is quite messy, however I do not have control over this.

My task is to read through all the folders. Retract the Meta data from the images and store in XML file. I then need to copy all the folders in the same layout and paste them in a new folder.

I think I will be able to read though all the directories and extract the meta data from the images and save it to an xml file.

What I do not know how to do is copy and past all the folders and images and paste them in a new directory maintaining the same folder structure.

Does anybody know of an efficient way to perform this task or is there any project, code available I could use as a starting point.

I am fairly new to C# and writing console apps.
Thanks for your time.

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

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

发布评论

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

评论(2

输什么也不输骨气 2024-11-16 04:35:15

递归解析目录

static void ParseDirectories(string root)
{
    ProcessDirectory(new DirectoryInfo(root));

    string[] subDirectories = Directory.GetDirectories(root);

    // No more directories to explore
    if (subDirectories.Length == 0)
        return;

    foreach (string subDirectory in subDirectories)
    {
        ParseDirectories(subDirectory);
    }
}

处理目录中的文件

static void ProcessDirectory(DirectoryInfo directory)
{
    foreach (FileInfo file in directory.EnumerateFiles("*.jpg")
    {
        // record metadata and do other work on each file here
    }
}

复制目录树

static void CopyDirectoryTree(DirectoryInfo source, DirectoryInfo dest)
{
    if (!Directory.Exists(dest.FullName))
        Directory.CreateDirectory(dest.FullName);

    bool overwrite = true;

    // Copy files
    foreach (FileInfo file in source.EnumerateFiles())
    {
        file.CopyTo(Path.Combine(dest.ToString(), file.Name), overwrite);
    }

    // Copy Sub-directories
    foreach (DirectoryInfo subDirectory in source.GetDirectories())
    {
        DirectoryInfo newDirectory = destination.CreateSubdirectory(subDirectory.Name);
        CopyDirectoryTree(subDirectory, newDirectory);
    }
}

示例用法

static void Main(string[] args)
{
    // Process each directory
    string initialDirectory = @"C:\path_to_folder";
    ParseDirectories(initialDirectory);

    // Copy directory tree
    string destinationDirectory = @"C:\path_to_new_root_directory";
    CopyDirectoryTree(
        new DirectoryInfo(initialDirectory),
        new DirectoryInfo(destinationDirectory));
}

希望这有帮助!

Parsing Directories Recursively

static void ParseDirectories(string root)
{
    ProcessDirectory(new DirectoryInfo(root));

    string[] subDirectories = Directory.GetDirectories(root);

    // No more directories to explore
    if (subDirectories.Length == 0)
        return;

    foreach (string subDirectory in subDirectories)
    {
        ParseDirectories(subDirectory);
    }
}

Processing the Files in a Directory

static void ProcessDirectory(DirectoryInfo directory)
{
    foreach (FileInfo file in directory.EnumerateFiles("*.jpg")
    {
        // record metadata and do other work on each file here
    }
}

Copying a Directory tree

static void CopyDirectoryTree(DirectoryInfo source, DirectoryInfo dest)
{
    if (!Directory.Exists(dest.FullName))
        Directory.CreateDirectory(dest.FullName);

    bool overwrite = true;

    // Copy files
    foreach (FileInfo file in source.EnumerateFiles())
    {
        file.CopyTo(Path.Combine(dest.ToString(), file.Name), overwrite);
    }

    // Copy Sub-directories
    foreach (DirectoryInfo subDirectory in source.GetDirectories())
    {
        DirectoryInfo newDirectory = destination.CreateSubdirectory(subDirectory.Name);
        CopyDirectoryTree(subDirectory, newDirectory);
    }
}

Sample usage

static void Main(string[] args)
{
    // Process each directory
    string initialDirectory = @"C:\path_to_folder";
    ParseDirectories(initialDirectory);

    // Copy directory tree
    string destinationDirectory = @"C:\path_to_new_root_directory";
    CopyDirectoryTree(
        new DirectoryInfo(initialDirectory),
        new DirectoryInfo(destinationDirectory));
}

Hope this helps!

锦欢 2024-11-16 04:35:15

我可以提出以下建议吗?在我看来,这更简单一些

public static void CopyFolderTree(string sourcePath, string targetPath)
{
    var sourceDir = new DirectoryInfo(sourcePath);
    var targetDir = new DirectoryInfo(targetPath);

    targetDir.Create();

    foreach(var file in sourceDir.GetFiles())
        file.CopyTo(Path.Combine(targetPath, file.Name), true);

    foreach(var subfolder in sourceDir.GetDirectories())
        CopyFolderTree(subfolder.FullName, Path.Combine(targetPath, subfolder.Name));
}

May I suggest the following, which is, in my opinion, a little bit more straightforward

public static void CopyFolderTree(string sourcePath, string targetPath)
{
    var sourceDir = new DirectoryInfo(sourcePath);
    var targetDir = new DirectoryInfo(targetPath);

    targetDir.Create();

    foreach(var file in sourceDir.GetFiles())
        file.CopyTo(Path.Combine(targetPath, file.Name), true);

    foreach(var subfolder in sourceDir.GetDirectories())
        CopyFolderTree(subfolder.FullName, Path.Combine(targetPath, subfolder.Name));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文