0
votes

This is how i'm checking and creating a directory if the directory is not exist:

private void MakeDir(string dirName)
        {
            FtpWebResponse response = null;
            Stream ftpStream = null;
            try
            {
                FtpWebRequest reqFTP;
                string[] Files = GetFileList();
                ArrayList arrDirectories = new ArrayList();
                if (Files != null)
                {
                    foreach (string dir in Files)
                    {
                        arrDirectories.Add(dir);
                    }
                }
                if (!arrDirectories.Contains(dirName))
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + f.Host + "/" + dirName));
                    reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                    reqFTP.UseBinary = true;
                    reqFTP.KeepAlive = false;
                    reqFTP.Proxy = null;
                    reqFTP.Credentials = new NetworkCredential(f.Username, f.Password);
                    response = (FtpWebResponse)reqFTP.GetResponse();
                    ftpStream = response.GetResponseStream();
                }
            }
            catch (Exception ex)
            {
                if (ftpStream != null)
                {
                    ftpStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
        }

And this is the method GetFileList:

public string[] GetFileList()
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            WebResponse response = null;
            StreamReader reader = null;
            try
            {
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + f.Host + "/"));
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(f.Username, f.Password);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                reqFTP.Proxy = null;
                reqFTP.KeepAlive = false;
                reqFTP.UsePassive = false;
                response = reqFTP.GetResponse();
                reader = new StreamReader(response.GetResponseStream());
                string line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }
                result.Remove(result.ToString().LastIndexOf('\n'), 1);
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
                downloadFiles = null;
                return downloadFiles;
            }
        }

In MakeDir i give a directory name for example the name is: D

and if the directory name D not exist it will create the directory D. Untill here there are no problems. But if the directory i give is D/Test1/Test2/Test3/Test4/Test5 and let's say that only D is exist then now i need to add somehow a recursive loop to the

And another situation might be that i have already a directory strcture: D/Test1/Test2/Test3 and i give a directory to create D/Test1/Test2/Test3/Test4/Test5 in this i know that D/Test1/Test2/Test3 already exist so i need to create only: D/Test1/Test2/Test3/Test4 and then D/Test1/Test2/Test3/Test4/Test5

But in all cases it should create it like this: D/Test1/Test2/Test3 will create Test3 under D/Test1/Test2 and D/Test1/Test2/Test3/Test4 will be under D/Test1/Test2/Test3.

How can i build the recursive loop in the MadeDir method ?

In the end the idea the goal is to create the directory structure recursively.

1
I don't see any recursion in methods you've shown. Shouldn't be there some looping?cyberhubert
That's my question how to add/make the recursion loop in the MakeDir method. Today the MakeDir method check if the directory exist and create the directory if not but it will not create a directory with childs directories like d/test1/test2/test3 it will create only directories like d or if the directory name is testing1 but if it's a strcture like testing1/testing2/testing3 that's where i need the recursion loop. My question is how to add/make the recursion loop.Jorge Hyyest
cyberhubert the recursion loop should be inside the MakeDir. If the directory name in the variable dirName is a structure not a single directory name but a structure like: d/test1/test2/test3 then i need the recursion loop in the MakeDir to create the structure.Jorge Hyyest

1 Answers

0
votes

forgetting the FTP part, since I don't think it makes a difference here, it should be something like this:

public void Sync(string src, string target){
    EnsureTargetExists(target);
    CopyFiles(src, target);
    foreach(var dir in GetDirectories(src)){
        Sync(src+"/"+dir, target+"/"+dir);//recurence call
    }
}

and that's all the recurrency you need.