0 votes
by (460 points)
edited

Hi,

I want to delete only EMPTY Folders in specific remote directory.

My directory structure is :

OUTBOX dir contains --> FTP dir & some files this FTp dir contains--> ABC dir & some files

I am downloading from OUTBOX using GetFiles() Recursively, when download completes i want to move these files and folders from OUTBOX to BACKUP dir.

I am able to move files only not folders so i want to delete empty folders.

I write code like this :

            Ftp ftpDelete = new Ftp();
            if (!ftpDelete.IsAuthenticated)
            {
                ftpDelete.Connect(strFtpHost, iFtpPort);
                ftpDelete.Login(strFtpUsrname, strFtpPwd);
            }

            if (strFtpTransferMode.ToUpper().Trim() == "ACTIVE")
            {
                ftpDelete.Passive = false;
            }
            else
            {
                ftpDelete.Passive = true;
            }
            ftpDelete.Timeout = 30000;

            FtpItemCollection items = ftpDelete.GetItems(new Rebex.IO.FileSet(path, "*", TraversalMode.Recursive));

            if (items.Count > 0)
            {
                foreach (var item in items)
                {
                    if (item.IsDirectory)
                    {
                        bool blExists = ftpDelete.DirectoryExists(item.Path);
                        if (blExists == true)
                        {
                            CheckAndDeleteRemoteDir(item.Path);
                        }
                    }
                }
            }
            if (items.Count == 0)
            {
                ftpDelete.Delete(path, TraversalMode.Recursive);
            }

But using above code i am not able to delete all empty folders.

Please help me to delete all empty folders except OUTBOX

Thanks & Regards Srinu

Applies to: Rebex FTP/SSL

4 Answers

0 votes
by (58.9k points)
edited
 
Best answer

Hello Srinu, the solution I provided before really did not look for empty directories in subdirectories (did not use recursion). This one does use recursion, so it is able to find empty directories under any subdirectory hierarchy, and delete them:

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

    private static void DeleteAllEmptyDirectories(Ftp ftp, string path)
    {
        string originalPath = ftp.GetCurrentDirectory();
        ftp.ChangeDirectory(path);

        FtpItemCollection list = ftp.GetList();
        foreach (FtpItem item in list)
        {
            if (item.IsDirectory)
            {
                ftp.ChangeDirectory(item.Name);
                //get the contents of current directory
                FtpItemCollection dirContents = ftp.GetList();

                //step out of current directory into 'path'
                ftp.ChangeDirectory("..");

                // check that directory is really empty.
                if (dirContents.Count == 0)
                {
                    ftp.RemoveDirectory(item.Name);// delete the empty directory
                }
                else
                {
                    //recursively delete empty directories
                    DeleteAllEmptyDirectories(ftp, item.Path);
                }
            }
        }

        //change working dir back to original
        ftp.ChangeDirectory(originalPath);
    }
by (460 points)
edited

Hi All,

Thanks for your valuble help. I am able to delete all Empty directories now

Regards

Srinu

0 votes
by (58.9k points)
edited

Hello, to delete empty directories from a certain path ("/OUTBOX"), try this piece of code:

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

        DeleteEmptyDirectories(ftp, "/OUTBOX");

    // deletes directories which are empty
    private static void DeleteEmptyDirectories(Ftp ftp, string path)
    {
        string originalPath = ftp.GetCurrentDirectory();
        ftp.ChangeDirectory(path);

        FtpItemCollection list = ftp.GetList();
        foreach (FtpItem item in list)
        {
            if (item.IsDirectory)
            {
                ftp.ChangeDirectory(item.Name);
                //get the contents of current directory
                FtpItemCollection dirContents = ftp.GetList();

                //step out of current directory into 'path'
                ftp.ChangeDirectory("..");

                // check that directory is really empty.
                if (dirContents.Count == 0)
                {
                    ftp.RemoveDirectory(item.Name);// delete the empty directory
                }
            }
        }

        //change working dir back to original
        ftp.ChangeDirectory(originalPath);              }
        }
    }
by (460 points)
edited

Hi Tomas,

Thanks for your help.

by (58.9k points)
edited

Hello Srinu, you are welcome.

0 votes
by (460 points)
edited

Hi Tomas,

With the above code am unable to delete empty folders.

After Download i want to delete the folders. So my Remote path is "/OUTBOX"

in your code i don't use

    string originalPath = ftp.GetCurrentDirectory(); 
    ftp.ChangeDirectory(path);

above 2 lines of code and also

    //change working dir back to original 
    ftp.ChangeDirectory(originalPath);

This line.

I am unable to delete the Empty folders.

Thanks & Regards Srinu

by (58.9k points)
edited

Sorry, the code above will not work without those lines. Does it work if you add those lines back? At least ftp.ChangeDirectory(path); has definitely to be there.

0 votes
by (460 points)
edited

Hi Tomas,

By putting that code also i am unable to delete the empty directorires.

I don't want to delete the one empty directory.

I want to delete all empty directories within the "/OUTBOX"

Thanks & Regards

Srinu

by (148k points)
edited

Hi, The code Tomas posted deletes all empty directories within "/OUTBOX" - for example "/OUTBOX/one" or "/OUTBOX/two" (if they are empty). If it doesn't work for you, it most likely means your FTP server is behaving in a strange way. Please use Ftp object's LogWriter property to create a communication log of Tomas's code (as described at http://www.rebex.net/kb/logging.aspx) and either post it here or mail it to support@rebex.net for analysis). Thanks!

...