Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
FTPClient How to recursively delete a folder with the files within?
Posted

Here is the source code that will get all the folders & file recursively from FTP
Now you need to modify this code as per your requirement.

C#
public static void GetFTPFilesPath(string ftpAddress, string UserName, string Password)
        {
           
            FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpAddress);

            try
            {
                reqFTP.UsePassive = true;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = false;
                reqFTP.Credentials = new NetworkCredential(UserName, Password);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

                Stream responseStream = response.GetResponseStream();
                List<string> files = new List<string>();
                StreamReader reader = new StreamReader(responseStream);
                while (!reader.EndOfStream)
                    files.Add(reader.ReadLine());
                reader.Close();
                responseStream.Dispose();
                
                //Loop through the resulting file names.
                string ftpPath = string.Empty;
                foreach (var fileName in files)
                {
                    var parentDirectory = "";
                    PeriodicalFile lstFile = new PeriodicalFile();
                    //If the filename has an extension, then it actually is 
                    //a file            
                    if (fileName.Contains(".zip"))
                    {
                        ftpPath = ftpAddress + fileName;
                        
                    }
                    else
                    {
                        //If the filename has no extension, then it is just a folder. 
                        //Run this method again as a recursion of the original:
                        parentDirectory += fileName + "/";
                        try
                        {
                            GetFTPFilesPath(ftpAddress + "/" + parentDirectory, UserName, Password);
                        }
                        catch (Exception ex)
                        {
                            ErrorLog.WriteLog("ftpFileProcessing", "GetFTPFilesPath(Else)", ex.Message, ex.StackTrace, "");
                        }
                    }
                }
                
            }
            

            catch (Exception excpt)
            {

                reqFTP.Abort();
                ErrorLog.WriteLog("ftpFileProcessing", "GetFTPFilesPath", excpt.Message, excpt.StackTrace, "");

            }           



        }
 
Share this answer
 
v2
Comments
Gishisoft 16-Jul-13 2:02am    
This is the example.

www.host.com/folder1/folder2/folder3/...

i want to delete the folder1. i want all the folder inside the folder1 will be deleted. how? i don't care what files inside the folder. assuming the files are *.*; that is random files.
 
Share this answer
 
Comments
Gishisoft 16-Jul-13 2:18am    
Sorry! Its ordinary delete directory i'm looking for the simple recursive delete directory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900