FTP:自动创建嵌套目录

发布于 2025-01-04 04:18:20 字数 379 浏览 2 评论 0原文

我们正在尝试将文件移动到 FTP 站点。我们需要创建一个像这样的文件夹结构:

/deeply/nested/folder/struct/index.html

昨天,我们意识到我们无法一次创建多个文件夹,所以这是行不通的:

MKD /深层/嵌套/文件夹/结构

因此,在代码中,我们编写了一个循环来创建每个文件夹,一次一个,忽略由已存在的文件夹引起的错误。忽视错误是很恶心的。

是否有一种方法可以通过一个操作而不是多个操作来创建这些嵌套文件夹?有没有命令可以查看文件夹是否已存在?如果我们只是推送包含完整路径的文件,FTP 是否足够智能来为我创建目录?

We are trying to move a file to an FTP site. We need to create a folder structure like this:

/deeply/nested/folder/structure/index.html

Yesterday, we realized we can't create more than one folder at a time, so this doesn't work:

MKD /deeply/nested/folder/structure

So, in code, we wrote a loop that created each folder, one at a time, ignoring errors caused by the folder already existing. Ignoring errors is gross.

Is a way to create these nested folders in one action, rather than multiple? Is there a command to see if a folder already exists? If we just push the file out including the full path, will FTP be smart enough to create the directories for me?

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

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

发布评论

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

评论(2

小红帽 2025-01-11 04:18:21

不,没有创建包含子文件夹的文件夹的标准方法。也没有标准方法来检查目录是否存在。您需要使用 LIST 或 MLSD(如果支持)并解析结果。您不能只使用一些支持所需功能的第三方组件吗?

No, there's no standard way to create a folder with subfolders. There's also no standard way to check if directory exists. You would need to use LIST or MLSD (where supported) and parse the result for this. Can't you just use some third-party component that supports the needed functionality?

梦里人 2025-01-11 04:18:21

我在这里写了一个简单的 C# 示例,也许有人会需要这段代码。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace Deployer
{
    public class RecursiveFolderUploader
    {
        private const string SrcFullPath = @"C:\prg\Snowinmars\Snowinmars.Ui";
        private readonly IList<string> foldersOnServer;
        private Client ftpClient; // that's your ftp client, init it

        public RecursiveFolderUploader()
        {
            this.foldersOnServer = new List<string>();
        }

        private void UploadAll(DirectoryInfo directoryInfo)
        {
            // ftp://login:[email protected]/path/to/root/mydir/anotherdir/file.dat
            // ^________________uri_______________________^_relevationPath_^
            foreach (var file in directoryInfo.EnumerateFiles())
            {
                if (!file.Directory.FullName.StartsWith(RecursiveFolderUploader.SrcFullPath))
                {
                    throw new InvalidOperationException($"File {file.FullName} is not from {RecursiveFolderUploader.SrcFullPath} folder");
                }

                string relevationPath; // all folders from root up to file

                if (file.Directory.FullName.Length == RecursiveFolderUploader.SrcFullPath.Length)
                {
                    relevationPath = "";
                }
                else
                {
                    relevationPath = file.Directory.FullName.Substring(RecursiveFolderUploader.SrcFullPath.Length, file.Directory.FullName.Length - RecursiveFolderUploader.SrcFullPath.Length);

                    if (relevationPath.StartsWith("\\"))
                    {
                        relevationPath = relevationPath.Remove(0, 1);
                    }
                }

                string destination;
                if (string.IsNullOrWhiteSpace(relevationPath))
                {
                    destination = file.Name;
                }
                else
                {
                    destination = Path.Combine(relevationPath, file.Name).Replace("\\", "/");
                }

                try
                {
                    ftpClient.UploadFile(file.FullName, destination);
                }
                catch (WebException e)
                {
                    // that means that there's no such folder or something else goes wrong
                    // we can check it by creating folders and try again
                    var parts = relevationPath.Replace("\\", "/").Split('/');

                    for (int i = 1; i <= parts.Length; i++)
                    {
                        var path = string.Join("/", parts.Take(i));

                        if (!foldersOnServer.Contains(path))
                        {
                            ftpClient.MakeDirectory(path);
                            foldersOnServer.Add(path);
                        }
                    }

                    try
                    {
                        ftpClient.UploadFile(file.FullName, destination);
                    }
                    catch (Exception innerE)
                    {
                        // if it doesn't help - trouble isn't in folders
                        throw new WebException($"Can't find folder {relevationPath}", innerE);
                    }
                }
            }

            foreach (var directory in directoryInfo.EnumerateDirectories())
            {
                UploadAll(directory);
            }
        }
    }
}

I wrote a simple C# example here, maybe someone will need this code.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace Deployer
{
    public class RecursiveFolderUploader
    {
        private const string SrcFullPath = @"C:\prg\Snowinmars\Snowinmars.Ui";
        private readonly IList<string> foldersOnServer;
        private Client ftpClient; // that's your ftp client, init it

        public RecursiveFolderUploader()
        {
            this.foldersOnServer = new List<string>();
        }

        private void UploadAll(DirectoryInfo directoryInfo)
        {
            // ftp://login:[email protected]/path/to/root/mydir/anotherdir/file.dat
            // ^________________uri_______________________^_relevationPath_^
            foreach (var file in directoryInfo.EnumerateFiles())
            {
                if (!file.Directory.FullName.StartsWith(RecursiveFolderUploader.SrcFullPath))
                {
                    throw new InvalidOperationException($"File {file.FullName} is not from {RecursiveFolderUploader.SrcFullPath} folder");
                }

                string relevationPath; // all folders from root up to file

                if (file.Directory.FullName.Length == RecursiveFolderUploader.SrcFullPath.Length)
                {
                    relevationPath = "";
                }
                else
                {
                    relevationPath = file.Directory.FullName.Substring(RecursiveFolderUploader.SrcFullPath.Length, file.Directory.FullName.Length - RecursiveFolderUploader.SrcFullPath.Length);

                    if (relevationPath.StartsWith("\\"))
                    {
                        relevationPath = relevationPath.Remove(0, 1);
                    }
                }

                string destination;
                if (string.IsNullOrWhiteSpace(relevationPath))
                {
                    destination = file.Name;
                }
                else
                {
                    destination = Path.Combine(relevationPath, file.Name).Replace("\\", "/");
                }

                try
                {
                    ftpClient.UploadFile(file.FullName, destination);
                }
                catch (WebException e)
                {
                    // that means that there's no such folder or something else goes wrong
                    // we can check it by creating folders and try again
                    var parts = relevationPath.Replace("\\", "/").Split('/');

                    for (int i = 1; i <= parts.Length; i++)
                    {
                        var path = string.Join("/", parts.Take(i));

                        if (!foldersOnServer.Contains(path))
                        {
                            ftpClient.MakeDirectory(path);
                            foldersOnServer.Add(path);
                        }
                    }

                    try
                    {
                        ftpClient.UploadFile(file.FullName, destination);
                    }
                    catch (Exception innerE)
                    {
                        // if it doesn't help - trouble isn't in folders
                        throw new WebException($"Can't find folder {relevationPath}", innerE);
                    }
                }
            }

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