0 votes
by (120 points)
edited

When I send a file to FTP is there a way to have the Rebex component to automatically create directories on server if they do not exist (similarly to what the BatchUpload does?)

Applies to: Rebex FTP/SSL

1 Answer

+2 votes
by (144k points)
edited

This is not supported by the component itself, but can be easily implemented. The following method should do what you need. It takes two arguments - an instance of Ftp object (connected and authenticated) and the path:

public static void CreatePath(Ftp ftp, string path)
{
    string[] parts = path.Trim('/').Split('/');
    if (parts.Length == 0)
        throw new ArgumentException("Invalid path.", "path");

    if (path.StartsWith("/"))
        path = "/";
    else
        path = "";

    string current = ftp.GetCurrentDirectory();
    for (int i = 0; i < parts.Length; i++)
    {
        path += parts[i];
        ftp.SendCommand("CWD " + path);
        FtpResponse response = ftp.ReadResponse();
        ftp.ChangeDirectory(current);
        if (response.Group == 5)
            ftp.CreateDirectory(path);
        path += "/";
    }
}

If you prefer VB.NET, please let me know.

...