How to get the root Directory after go to sub directories

+1 vote
asked Apr 4, 2013 by MSVSR (130 points)
edited Dec 12, 2014

Hi Can I get the FTP root directory when i working in Sub directories of FTP or how can i store the ftp root directory value constantly as I need to use it in the middle of the program.

Please prefer vb.net

My Heart full thanks to Replies

Applies to: Rebex FTP/SSL

4 Answers

0 votes
answered Apr 4, 2013 by Tomas Knopp (58,890 points)
edited Dec 12, 2014

Most FTP servers use remote filesystem with a single root accesible at "/". So you could use ChangeDirectory("/") to switch into FTP server's root directory like this:

' create client, connect and log in
Dim client As New Ftp
client.Connect("ftp.example.org")
client.Login("username", "password")

' change the current directory to root
client.ChangeDirectory("/")

client.Disconnect()
commented Dec 11, 2014 by ganeshspy (900 points)
edited Dec 11, 2014

Any other way to get root directory.?

commented Dec 11, 2014 by Tomas Knopp (58,890 points)
edited Dec 11, 2014

Could you please share with us what is the thing you want to achieve? And why is changing into the root with the ChangeDirectory("/") command not suitable for you?

commented Dec 12, 2014 by Tomas Knopp (58,890 points)
edited Dec 12, 2014

@ganeshspy I have come up with another method to detect the root directory. See this answer.

0 votes
answered Apr 6, 2013 by MSVSR1 (160 points)
edited Dec 12, 2014

Hello Tomas

Thanks for your reply. If the root directory is "/" then we can use client.ChangeDirectory("/") scenario. But all the ftp root directories must not be "/", in such case what we have to do?

Here is my actual problem...

I am dynamically creating the download path, that I need to concatenate with the root directory to download. This step will be continued until some condition. But when I changed the current path to path that I created for download the files, it is not working for next step......

Would you help me please?

Thanks

commented Dec 12, 2014 by Tomas Knopp (58,890 points)
edited Dec 12, 2014

In fact almost any normal FTP server running on Unix or Windows would have the "/" as a root. See this answer for more details.

+2 votes
answered Apr 6, 2013 by Tomas Knopp (58,890 points)
edited Dec 11, 2014

If you need to programmatically get the home directory name, you could use GetCurrentDirectory method like this: (provided root is the current directory after you login.)

        Ftp ftp = new Ftp();
        ftp.Connect("server");
        ftp.Login("user","password");

        string homeDir = ftp.GetCurrentDirectory();

        // do work

        ftp.Disconnect();
commented Apr 8, 2013 by MSVSR1 (160 points)
edited Dec 11, 2014

I got it.

Thanks Tomas,

commented Dec 11, 2014 by ganeshspy (900 points)
edited Dec 11, 2014

Any other way to get root directory in same connection ?

commented Dec 11, 2014 by Tomas Knopp (58,890 points)
edited Dec 11, 2014

Sorry the code above actually detects the home direktory, not the root direktory. However, detecting the home dir helped the user. I have edited my answer to correct the mistake.

+1 vote
answered Dec 12, 2014 by Tomas Knopp (58,890 points)
edited Dec 12, 2014

Unfortunatelly, there is no firm standard stating how the root directory of an FTP server should be named, nor even whether there has to be a root directory.

In fact almost any normal FTP server running on Unix or Windows would have the "/" as a root.

Theoretically, there might be FTP servers with a strange root directory name other than "/" that still use the concept of directory (so you can go out of the directory with the ftp.ChangeDirectory("..") or some other command which you have to know). For these servers I have come up with an algorithm to detect root. Basically it tries to jump out of the current directory and if the directory after the jump is the same, then it is the root. The maximum number of attempts is applied to make sure the algorithm ends even for the weirdest FTP servers.

Here is the DetectFtpRoot method, it will work also for normal FTP servers with the most commion "/" root dir:

    string DetectFtpRoot(Ftp ftp)
    {
        string up = "..", oldDir, currentDir;
        int maxTries = 100;
        int i = 0;
        string root = null;

        oldDir = ftp.GetCurrentDirectory();

        while (i < maxTries)
        {
            ftp.ChangeDirectory(up);
            currentDir = ftp.GetCurrentDirectory();
            i++;

            if (oldDir == currentDir)
            {
                root = oldDir;
                break;
            }
            oldDir = currentDir;
        }

        if (root == null)
            throw new Exception("Unable to detect FTP server root, maximum retries number reached.");

        return root;
    }

Please note that there are also some weird FTP servers out there that for instance even do not use the concept of "directories" so in this case I think there is no point talking about any concept of 'root' directory here at all. For these FTP servers the algorithm above will not work for obvious reason - the ChangeDirectory("..") method will have no effect. The above algorithm also will not work, if you do not know how the FTP server names the parent directory (".."). We do not know about any means to detect the root directory in these cases. This also applies to servers which do not support the ChangeDirectory("..") method.

...