Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recursively delete a folder with the files within using FtpWebRequest?

I want to delete a folder in FTP and it's files recursively.

Any example code do I can implement?

like image 541
Prince OfThief Avatar asked Feb 01 '11 19:02

Prince OfThief


People also ask

What is recursive deleting?

To delete files recursively means to delete the contents of the folder before deleting the folder itself. If the folder has other folders in it, it will do the same with those folders. Basically it means delete whatever is inside the folder I am deleting, so that I can delete the folder itself.


2 Answers

First you have to list all your files in your directory :

public static List<string> DirectoryListing(string Path, string ServerAdress, string Login, string Password)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ServerAdress + Path);
        request.Credentials = new NetworkCredential(Login, Password);

        request.Method = WebRequestMethods.Ftp.ListDirectory;            

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);

        List<string> result = new List<string>();

        while (!reader.EndOfStream)
        {
            result.Add(reader.ReadLine());
        }

        reader.Close();
        response.Close();

        return result;
    }

Then you need a method to delete a single file (because you can delete a folder only if it's empty) :

public static void DeleteFTPFile(string Path, string ServerAdress, string Login, string Password)
    {
        FtpWebRequest clsRequest = (System.Net.FtpWebRequest)WebRequest.Create("ftp://" + ServerAdress + Path);
        clsRequest.Credentials = new System.Net.NetworkCredential(Login, Password);

        clsRequest.Method = WebRequestMethods.Ftp.DeleteFile;

        string result = string.Empty;
        FtpWebResponse response = (FtpWebResponse)clsRequest.GetResponse();
        long size = response.ContentLength;
        Stream datastream = response.GetResponseStream();
        StreamReader sr = new StreamReader(datastream);
        result = sr.ReadToEnd();
        sr.Close();
        datastream.Close();
        response.Close();
    }

And finally :

public static void DeleteFTPDirectory(string Path, string ServerAdress, string Login, string Password)
{
        FtpWebRequest clsRequest = (System.Net.FtpWebRequest)WebRequest.Create("ftp://" + ServerAdress + Path);
        clsRequest.Credentials = new System.Net.NetworkCredential(Login, Password);

        List<string> filesList = DirectoryListing(Path, ServerAdress, Login, Password);

        foreach (string file in filesList)
        {
            DeleteFTPFile(Path + file, ServerAdress, Login, Password);
        }

        clsRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;

        string result = string.Empty;
        FtpWebResponse response = (FtpWebResponse)clsRequest.GetResponse();
        long size = response.ContentLength;
        Stream datastream = response.GetResponseStream();
        StreamReader sr = new StreamReader(datastream);
        result = sr.ReadToEnd();
        sr.Close();
        datastream.Close();
        response.Close();
    } 

You can easily call this like that (for me those methods are in a class called "Ftp") :

Ftp.DeleteFTPDirectory(the_path_of_your_folder_in_ftp,your_server_address,your_ftp_login,your_ftp_password);

Of course, you'll need to customize those lines, but it worked perfectly for me :)

like image 97
Julien Rubiano Avatar answered Oct 20 '22 16:10

Julien Rubiano


There's no support for recursive operations in the FtpWebRequest class (or any other FTP implementation in the .NET framework). You have to implement the recursion yourself:

  • List the remote directory
  • Iterate the entries, deleting files and recursing into subdirectories (listing them again, etc.)

Tricky part is to identify files from subdirectories. There's no way to do that in a portable way with the FtpWebRequest. The FtpWebRequest unfortunately does not support the MLSD command, which is the only portable way to retrieve directory listing with file attributes in FTP protocol. See also Checking if object on FTP server is file or directory.

Your options are:

  • Do an operation on a file name that is certain to fail for a file and to succeed for directory (or vice versa). I.e. you can try to download the "name". If that succeeds, it's a file, if that fails, it's a directory. But that can become a performance problem, when you have a large number of entries.
  • You may be lucky and in your specific case, you can tell a file from a directory by a file name (i.e. all your files have an extension, while subdirectories do not)
  • You use a long directory listing (LIST command = ListDirectoryDetails method) and try to parse a server-specific listing. Many FTP servers use *nix-style listing, where you identify a directory by the d at the very beginning of the entry. But many servers use a different format. The following example uses this approach (assuming the *nix format).
  • In this specific case, you can just try to delete the entry as a file. If deleting fails, try to list the entry as a directory. If the listing succeeds, you assume it's a folder and proceed accordingly. Unfortunately some servers do not error, when you try to list a file. They will just return a listing with a single entry for the file.
static void DeleteFtpDirectory(string url, NetworkCredential credentials)
{
    FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
    listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    listRequest.Credentials = credentials;

    List<string> lines = new List<string>();

    using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
    using (Stream listStream = listResponse.GetResponseStream())
    using (StreamReader listReader = new StreamReader(listStream))
    {
        while (!listReader.EndOfStream)
        {
            lines.Add(listReader.ReadLine());
        }
    }

    foreach (string line in lines)
    {
        string[] tokens =
          line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
        string name = tokens[8];
        string permissions = tokens[0];

        string fileUrl = url + name;

        if (permissions[0] == 'd')
        {
            DeleteFtpDirectory(fileUrl + "/", credentials);
        }
        else
        {
            FtpWebRequest deleteRequest = (FtpWebRequest)WebRequest.Create(fileUrl);
            deleteRequest.Method = WebRequestMethods.Ftp.DeleteFile;
            deleteRequest.Credentials = credentials;

            deleteRequest.GetResponse();
        }
    }

    FtpWebRequest removeRequest = (FtpWebRequest)WebRequest.Create(url);
    removeRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
    removeRequest.Credentials = credentials;

    removeRequest.GetResponse();
}

The url should be like ftp://example.com/directory/to/delete/


Or use a 3rd party library that supports recursive operations.

For example with WinSCP .NET assembly you can delete whole directory with a single call to Session.RemoveFiles:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Delete folder
    session.RemoveFiles("/home/user/foldertoremove").Check();
} 

Internally, WinSCP uses the MLSD command, if supported by the server. If not, it uses the LIST command and supports dozens of different listing formats.

(I'm the author of WinSCP)

like image 5
Martin Prikryl Avatar answered Oct 20 '22 16:10

Martin Prikryl