Automatically create folders

0 votes
asked Feb 9, 2010 by Andrea (120 points)
edited Mar 11, 2010

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

+1 vote
answered Feb 9, 2010 by Lukas Pokorny (92,630 points)
edited Feb 9, 2010

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.

...