4

I got a small problem on how to create a directory with subdirectories in a single ftp request

I have a string s

    string s = "a/b/c/d"

NOTE : The words between slashes are random and the number of items is unknown.

How to create in the FTP server the directory a/b/c/d ????

The way i'm using to achieve this is to split the string and create a folder for each part using the code below :

    var ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://domain.com/public_html/a");
    ftpWebRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
    ftpWebRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
    ftpWebRequest.GetResponse();

Then i create the b directory inside a, then c inside b, then d inside c by repeating some code, each time

I tried to type the url directly. like :

    var ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://doemin.com/public_html/a/b/c/d);

but it doesn't work.

Is there a short way on how can i create a folder with other subdirectories in a one single request ?

Rafik Bari
  • 4,867
  • 18
  • 73
  • 123
  • 1
    Do you consider using a free open source ftp library instead of native framework FtpWebRequest class? – Steve Jan 16 '13 at 22:28
  • Why not use a free FTP library: http://stackoverflow.com/questions/1371964/free-ftp-library. Will probably be a lot easier, and I'm sure one of them handles this situation out-of-the-box in a single call. – mellamokb Jan 16 '13 at 22:28

2 Answers2

1

If you are willing to use a more friendly library (free and open source) like this one:

System.Net.FtpClient.dll

then you could write code like this (adapted from their example)

static ManualResetEvent m_reset = new ManualResetEvent(false);
void Main()
{
    m_reset.Reset();
    using (FtpClient ftp = new FtpClient())
    {
        ftp.Host = "yourFTPHost.com";
        ftp.Credentials = new NetworkCredential("yourUserName", "yourPassword");
        ftp.SetWorkingDirectory("/rootForTest");
        if(ftp.DirectoryExists("test"))
            ftp.DeleteDirectory("test", true);
        ftp.BeginCreateDirectory("test/path/that/should/be/created", true,
                    new AsyncCallback(CreateDirectoryCallback), ftp);
        m_reset.WaitOne();
        ftp.Disconnect();
    }
}
static void CreateDirectoryCallback(IAsyncResult ar) 
{
    FtpClient ftp = ar.AsyncState as FtpClient;
    try 
    {
        if (ftp == null)
             throw new InvalidOperationException("The FtpControlConnection object is null!");
        ftp.EndCreateDirectory(ar);
    }
    catch (Exception ex) 
    {
        Console.WriteLine(ex.ToString());
    }
    finally 
    {
        m_reset.Set();
    }
}

A side note: System.Net.FtpClient requires the full NET 4.0 Framework. (Cliente Profile is not enough)

Steve
  • 213,761
  • 22
  • 232
  • 286
1

Some servers do support FTP commands like MKD a/b/c/d . If your server doesn't, but supports execution of shell commands via SITE command, you can try to call "SITE md a/b/c/d" (but this is machine-specific). If none of the above works, then you have to create folders in a loop like you do or use some library which hides this loop in a one method.

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121